comparison transports/websocket.js @ 106:c06ec02217ee

many changes
author Sonny Piers <sonny@fastmail.net>
date Tue, 26 Jun 2012 12:02:14 +0200
parents 3e124209821a
children 5cb4733c5189
comparison
equal deleted inserted replaced
105:fb50311997b5 106:c06ec02217ee
1 'use strict'; 1 'use strict';
2 2
3 (function() { 3 (function() {
4 Lightstring.WebSocketConnection = function(aService) { 4 Lightstring.WebSocket = WebSocket || MozWebSocket || undefined;
5 Lightstring.WebSocketConnection = function(aService, aJid) {
5 this.service = aService; 6 this.service = aService;
7 this.jid = aJid;
6 }; 8 };
7 Lightstring.WebSocketConnection.prototype = new EventEmitter(); 9 Lightstring.WebSocketConnection.prototype = new EventEmitter();
8 Lightstring.WebSocketConnection.prototype.open = function() { 10 Lightstring.WebSocketConnection.prototype.open = function() {
9 // Standard 11 if(!Lightstring.WebSocket)
10 if (typeof(WebSocket) === 'function')
11 this.socket = new WebSocket(this.service, 'xmpp');
12 // Safari
13 else if (typeof(WebSocket) === 'object')
14 this.socket = new WebSocket(this.service, 'xmpp');
15 // Old Gecko
16 else if (typeof(MozWebSocket) === 'function')
17 this.socket = new MozWebSocket(this.service, 'xmpp');
18 // No WebSocket support
19 else
20 return; //TODO: error 12 return; //TODO: error
13
14 this.socket = new WebSocket(this.service, 'xmpp');
21 15
22 var that = this; 16 var that = this;
23 this.socket.addEventListener('open', function() { 17 this.socket.addEventListener('open', function() {
24 //FIXME: Opera/Safari WebSocket implementation doesn't support sub-protocol mechanism. 18 //FIXME: Opera/Safari WebSocket implementation doesn't support sub-protocol mechanism.
25 //if (this.protocol !== 'xmpp') 19 //if (this.protocol !== 'xmpp')
26 //return; //TODO: error 20 //return; //TODO: error
27 that.emit('open'); 21 that.emit('open');
28 22
29 var stream = Lightstring.stanzas.stream.open(that.jid.domain); 23 var stream = Lightstring.stanzas.stream.open(that.jid.domain);
30 this.socket.send(stream); 24 var stanza = new Lightstring.Stanza();
31 var stanza = { 25 stanza.toString = function() {
32 XML: stream 26 return stream;
33 }; 27 }
34 that.emit('out', stanza); 28 that.send(stanza);
35 }); 29 });
36 this.socket.addEventListener('error', function(e) { 30 this.socket.addEventListener('error', function(e) {
37 that.emit('disconnecting', e.data); 31 that.emit('disconnecting', e.data);
38 //TODO: error 32 //TODO: error
39 }); 33 });
40 this.socket.addEventListener('close', function(e) { 34 this.socket.addEventListener('close', function(e) {
41 that.emit('disconnected', e.data); 35 that.emit('disconnected', e.data);
42 }); 36 });
43 this.socket.addEventListener('message', function(e) { 37 this.socket.addEventListener('message', function(e) {
44 that.emit('in', e.data); 38 var stanza = new Lightstring.Stanza(e.data);
39 that.emit('in', stanza);
45 }); 40 });
46 }; 41 };
47 Lightstring.WebSocketConnection.prototype.send = function(aStanza) { 42 Lightstring.WebSocketConnection.prototype.send = function(aStanza) {
48 this.socket.send(aStanza); 43 this.emit('out', aStanza);
49 that.emit('out', aStanza); 44 this.socket.send(aStanza.toString());
50 }; 45 };
51 })(); 46 })();