Mercurial > eldonilo > lightstring
comparison 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 |
comparison
equal
deleted
inserted
replaced
107:704ce44c1a22 | 108:5cb4733c5189 |
---|---|
1 'use strict'; | 1 'use strict'; |
2 | 2 |
3 (function() { | 3 (function() { |
4 Lightstring.WebSocket = WebSocket || MozWebSocket || undefined; | 4 var WebSocket = window.WebSocket || window.MozWebSocket || undefined; |
5 Lightstring.WebSocketConnection = function(aService, aJid) { | 5 |
6 if (typeof define !== 'undefined') { | |
7 define(function() { | |
8 return WebSocketTransport; | |
9 }); | |
10 } | |
11 else { | |
12 Lightstring.WebSocketTransport = WebSocketTransport; | |
13 } | |
14 | |
15 var WebSocketTransport = function(aService, aJid) { | |
6 this.service = aService; | 16 this.service = aService; |
7 this.jid = aJid; | 17 this.jid = aJid; |
8 }; | 18 }; |
9 Lightstring.WebSocketConnection.prototype = new EventEmitter(); | 19 WebSocketTransport.prototype = new EventEmitter(); |
10 Lightstring.WebSocketConnection.prototype.open = function() { | 20 WebSocketTransport.prototype.open = function() { |
11 if(!Lightstring.WebSocket) | 21 if(!WebSocket) |
12 return; //TODO: error | 22 return; //TODO: error |
13 | 23 |
14 this.socket = new WebSocket(this.service, 'xmpp'); | 24 this.socket = new WebSocket(this.service, 'xmpp'); |
15 | 25 |
16 var that = this; | 26 var that = this; |
17 this.socket.addEventListener('open', function() { | 27 this.socket.addEventListener('open', function() { |
18 //FIXME: Opera/Safari WebSocket implementation doesn't support sub-protocol mechanism. | 28 if (this.protocol !== 'xmpp') |
19 //if (this.protocol !== 'xmpp') | 29 ; //TODO: warning (Opera and Safari doesn't support this property) |
20 //return; //TODO: error | 30 |
21 that.emit('open'); | 31 that.emit('open'); |
22 | 32 |
23 var stream = Lightstring.stanzas.stream.open(that.jid.domain); | 33 var stream = Lightstring.stanzas.stream.open(that.jid.domain); |
24 var stanza = new Lightstring.Stanza(); | 34 var stanza = new Lightstring.Stanza(); |
25 stanza.toString = function() { | 35 stanza.toString = function() { |
37 this.socket.addEventListener('message', function(e) { | 47 this.socket.addEventListener('message', function(e) { |
38 var stanza = new Lightstring.Stanza(e.data); | 48 var stanza = new Lightstring.Stanza(e.data); |
39 that.emit('in', stanza); | 49 that.emit('in', stanza); |
40 }); | 50 }); |
41 }; | 51 }; |
42 Lightstring.WebSocketConnection.prototype.send = function(aStanza) { | 52 WebSocketTransport.prototype.send = function(aStanza) { |
43 this.emit('out', aStanza); | 53 this.emit('out', aStanza); |
44 this.socket.send(aStanza.toString()); | 54 this.socket.send(aStanza.toString()); |
45 }; | 55 }; |
56 | |
46 })(); | 57 })(); |