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