annotate transports/websocket.js @ 107:704ce44c1a22

Add several properties to the Lightstring.Stanza object
author Sonny Piers <sonny@fastmail.net>
date Thu, 28 Jun 2012 12:51:04 +0200
parents c06ec02217ee
children 5cb4733c5189
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
100
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
1 'use strict';
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
2
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
3 (function() {
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
4 Lightstring.WebSocket = WebSocket || MozWebSocket || undefined;
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
5 Lightstring.WebSocketConnection = function(aService, aJid) {
100
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
6 this.service = aService;
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
7 this.jid = aJid;
100
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
8 };
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
9 Lightstring.WebSocketConnection.prototype = new EventEmitter();
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
10 Lightstring.WebSocketConnection.prototype.open = function() {
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
11 if(!Lightstring.WebSocket)
100
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
12 return; //TODO: error
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
13
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
14 this.socket = new WebSocket(this.service, 'xmpp');
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
15
100
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
16 var that = this;
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
17 this.socket.addEventListener('open', function() {
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
18 //FIXME: Opera/Safari WebSocket implementation doesn't support sub-protocol mechanism.
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
19 //if (this.protocol !== 'xmpp')
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
20 //return; //TODO: error
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
21 that.emit('open');
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
22
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
23 var stream = Lightstring.stanzas.stream.open(that.jid.domain);
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
24 var stanza = new Lightstring.Stanza();
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
25 stanza.toString = function() {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
26 return stream;
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
27 }
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
28 that.send(stanza);
100
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
29 });
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
30 this.socket.addEventListener('error', function(e) {
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
31 that.emit('disconnecting', e.data);
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
32 //TODO: error
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
33 });
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
34 this.socket.addEventListener('close', function(e) {
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
35 that.emit('disconnected', e.data);
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
36 });
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
37 this.socket.addEventListener('message', function(e) {
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
38 var stanza = new Lightstring.Stanza(e.data);
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
39 that.emit('in', stanza);
100
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
40 });
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
41 };
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
42 Lightstring.WebSocketConnection.prototype.send = function(aStanza) {
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
43 this.emit('out', aStanza);
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
44 this.socket.send(aStanza.toString());
100
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
45 };
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
46 })();