Mercurial > eldonilo > lightstring
annotate websocket.js @ 97:11e38a9bfe38
multi-transport is now possible
author | Sonny Piers <sonny.piers@gmail.com> |
---|---|
date | Tue, 12 Jun 2012 16:35:27 +0200 |
parents | |
children | 6ec16b3e9cfc |
rev | line source |
---|---|
97
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
1 'use strict'; |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
2 |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
3 (function() { |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
4 Lightstring.WebSocketConnection = function(aService) { |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
5 this.service = aService; |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
6 }; |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
7 Lightstring.WebSocketConnection.prototype = new EventEmitter(); |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
8 Lightstring.WebSocketConnection.prototype.connect = function() { |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
9 // Standard |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
10 if (typeof(WebSocket) === 'function') |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
11 this.socket = new WebSocket(this.service, 'xmpp'); |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
12 // Safari |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
13 else if (typeof(WebSocket) === 'object') |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
14 this.socket = new WebSocket(this.service, 'xmpp'); |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
15 // Old Gecko |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
16 else if (typeof(MozWebSocket) === 'function') |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
17 this.socket = new MozWebSocket(this.service, 'xmpp'); |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
18 // No WebSocket support |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
19 else |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
20 return; //TODO: error |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
21 |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
22 var Conn = this; |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
23 this.socket.addEventListener('open', function() { |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
24 //FIXME: Opera/Safari WebSocket implementation doesn't support sub-protocol mechanism. |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
25 //if (this.protocol !== 'xmpp') |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
26 //return; //TODO: error |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
27 Conn.emit('open') |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
28 }); |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
29 this.socket.addEventListener('error', function(e) { |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
30 Conn.emit('disconnecting', e.data); |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
31 //TODO: error |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
32 }); |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
33 this.socket.addEventListener('close', function(e) { |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
34 Conn.emit('disconnected', e.data); |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
35 }); |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
36 this.socket.addEventListener('message', function(e) { |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
37 Conn.emit('stanza', e.data); |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
38 }); |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
39 }, |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
40 Lightstring.WebSocketConnection.prototype.send = function(aStanza) { |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
41 this.socket.send(aStanza); |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
42 } |
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
43 })(); |