Mercurial > eldonilo > lightstring
annotate 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 |
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() { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
4 Lightstring.WebSocketConnection = function(aService) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
5 this.service = aService; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
6 }; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
7 Lightstring.WebSocketConnection.prototype = new EventEmitter(); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
8 Lightstring.WebSocketConnection.prototype.open = function() { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
9 // Standard |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
10 if (typeof(WebSocket) === 'function') |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
11 this.socket = new WebSocket(this.service, 'xmpp'); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
12 // Safari |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
13 else if (typeof(WebSocket) === 'object') |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
14 this.socket = new WebSocket(this.service, 'xmpp'); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
15 // Old Gecko |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
16 else if (typeof(MozWebSocket) === 'function') |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
17 this.socket = new MozWebSocket(this.service, 'xmpp'); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
18 // No WebSocket support |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
19 else |
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 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
22 var that = this; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
23 this.socket.addEventListener('open', function() { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
24 //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
|
25 //if (this.protocol !== 'xmpp') |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
26 //return; //TODO: error |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
27 that.emit('open'); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
28 |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
29 var stream = Lightstring.stanzas.stream.open(that.jid.domain); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
30 this.socket.send(stream); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
31 var stanza = { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
32 XML: stream |
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 that.emit('out', stanza); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
35 }); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
36 this.socket.addEventListener('error', function(e) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
37 that.emit('disconnecting', e.data); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
38 //TODO: error |
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('close', function(e) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
41 that.emit('disconnected', e.data); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
42 }); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
43 this.socket.addEventListener('message', function(e) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
44 that.emit('in', e.data); |
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 }; |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
47 Lightstring.WebSocketConnection.prototype.send = function(aStanza) { |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
48 this.socket.send(aStanza); |
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
49 that.emit('out', aStanza); |
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 })(); |