Mercurial > eldonilo > lightstring
comparison transports/websocket.js @ 100:3e124209821a
move bosh.js and websocket.js to transports/
author | Sonny Piers <sonny.piers@gmail.com> |
---|---|
date | Tue, 12 Jun 2012 19:46:52 +0200 |
parents | |
children | c06ec02217ee |
comparison
equal
deleted
inserted
replaced
99:f14558915187 | 100:3e124209821a |
---|---|
1 'use strict'; | |
2 | |
3 (function() { | |
4 Lightstring.WebSocketConnection = function(aService) { | |
5 this.service = aService; | |
6 }; | |
7 Lightstring.WebSocketConnection.prototype = new EventEmitter(); | |
8 Lightstring.WebSocketConnection.prototype.open = function() { | |
9 // Standard | |
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 | |
21 | |
22 var that = this; | |
23 this.socket.addEventListener('open', function() { | |
24 //FIXME: Opera/Safari WebSocket implementation doesn't support sub-protocol mechanism. | |
25 //if (this.protocol !== 'xmpp') | |
26 //return; //TODO: error | |
27 that.emit('open'); | |
28 | |
29 var stream = Lightstring.stanzas.stream.open(that.jid.domain); | |
30 this.socket.send(stream); | |
31 var stanza = { | |
32 XML: stream | |
33 }; | |
34 that.emit('out', stanza); | |
35 }); | |
36 this.socket.addEventListener('error', function(e) { | |
37 that.emit('disconnecting', e.data); | |
38 //TODO: error | |
39 }); | |
40 this.socket.addEventListener('close', function(e) { | |
41 that.emit('disconnected', e.data); | |
42 }); | |
43 this.socket.addEventListener('message', function(e) { | |
44 that.emit('in', e.data); | |
45 }); | |
46 }; | |
47 Lightstring.WebSocketConnection.prototype.send = function(aStanza) { | |
48 this.socket.send(aStanza); | |
49 that.emit('out', aStanza); | |
50 }; | |
51 })(); |