annotate websocket.js @ 99:f14558915187

bosh support
author Sonny Piers <sonny.piers@gmail.com>
date Tue, 12 Jun 2012 19:44:53 +0200
parents 6ec16b3e9cfc
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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();
98
6ec16b3e9cfc improvements on the websocket transport
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
8 Lightstring.WebSocketConnection.prototype.open = function() {
97
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
98
6ec16b3e9cfc improvements on the websocket transport
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
22 var that = this;
97
11e38a9bfe38 multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
23 this.socket.addEventListener('open', function() {
98
6ec16b3e9cfc improvements on the websocket transport
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
24 //FIXME: Opera/Safari WebSocket implementation doesn't support sub-protocol mechanism.
6ec16b3e9cfc improvements on the websocket transport
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
25 //if (this.protocol !== 'xmpp')
6ec16b3e9cfc improvements on the websocket transport
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
26 //return; //TODO: error
6ec16b3e9cfc improvements on the websocket transport
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
27 that.emit('open');
6ec16b3e9cfc improvements on the websocket transport
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
28
6ec16b3e9cfc improvements on the websocket transport
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
29 var stream = Lightstring.stanzas.stream.open(that.jid.domain);
6ec16b3e9cfc improvements on the websocket transport
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
30 this.socket.send(stream);
6ec16b3e9cfc improvements on the websocket transport
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
31 var stanza = {
6ec16b3e9cfc improvements on the websocket transport
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
32 XML: stream
6ec16b3e9cfc improvements on the websocket transport
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
33 };
6ec16b3e9cfc improvements on the websocket transport
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
34 that.emit('out', stanza);
97
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('error', function(e) {
98
6ec16b3e9cfc improvements on the websocket transport
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
37 that.emit('disconnecting', e.data);
97
11e38a9bfe38 multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
38 //TODO: error
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 this.socket.addEventListener('close', function(e) {
98
6ec16b3e9cfc improvements on the websocket transport
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
41 that.emit('disconnected', e.data);
97
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 this.socket.addEventListener('message', function(e) {
98
6ec16b3e9cfc improvements on the websocket transport
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
44 that.emit('in', e.data);
97
11e38a9bfe38 multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
45 });
98
6ec16b3e9cfc improvements on the websocket transport
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
46 };
97
11e38a9bfe38 multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
47 Lightstring.WebSocketConnection.prototype.send = function(aStanza) {
11e38a9bfe38 multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
48 this.socket.send(aStanza);
98
6ec16b3e9cfc improvements on the websocket transport
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
49 that.emit('out', aStanza);
6ec16b3e9cfc improvements on the websocket transport
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
50 };
97
11e38a9bfe38 multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
51 })();