annotate transports/websocket.js @ 108:5cb4733c5189

many api changes
author Sonny Piers <sonny@fastmail.net>
date Fri, 13 Jul 2012 15:26:18 +0200
parents c06ec02217ee
children
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() {
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
4 var WebSocket = window.WebSocket || window.MozWebSocket || undefined;
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
5
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
6 if (typeof define !== 'undefined') {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
7 define(function() {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
8 return WebSocketTransport;
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
9 });
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
10 }
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
11 else {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
12 Lightstring.WebSocketTransport = WebSocketTransport;
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
13 }
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
14
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
15 var WebSocketTransport = function(aService, aJid) {
100
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
16 this.service = aService;
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
17 this.jid = aJid;
100
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
18 };
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
19 WebSocketTransport.prototype = new EventEmitter();
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
20 WebSocketTransport.prototype.open = function() {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
21 if(!WebSocket)
100
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
22 return; //TODO: error
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
23
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
24 this.socket = new WebSocket(this.service, 'xmpp');
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
25
100
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
26 var that = this;
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
27 this.socket.addEventListener('open', function() {
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
28 if (this.protocol !== 'xmpp')
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
29 ; //TODO: warning (Opera and Safari doesn't support this property)
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
30
100
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
31 that.emit('open');
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
32
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
33 var stream = Lightstring.stanzas.stream.open(that.jid.domain);
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
34 var stanza = new Lightstring.Stanza();
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
35 stanza.toString = function() {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
36 return stream;
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
37 }
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
38 that.send(stanza);
100
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
39 });
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
40 this.socket.addEventListener('error', function(e) {
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
41 that.emit('disconnecting', e.data);
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
42 //TODO: error
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
43 });
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
44 this.socket.addEventListener('close', function(e) {
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
45 that.emit('disconnected', e.data);
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
46 });
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
47 this.socket.addEventListener('message', function(e) {
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
48 var stanza = new Lightstring.Stanza(e.data);
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
49 that.emit('in', stanza);
100
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
50 });
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
51 };
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
52 WebSocketTransport.prototype.send = function(aStanza) {
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
53 this.emit('out', aStanza);
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 100
diff changeset
54 this.socket.send(aStanza.toString());
100
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
55 };
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
56
100
3e124209821a move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
57 })();