Mercurial > eldonilo > lightstring
annotate transports/websocket.js @ 109:cd1f57661439 default tip
Fix files permissions.
| author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
|---|---|
| date | Wed, 01 Aug 2012 23:18:03 +0200 |
| parents | 5cb4733c5189 |
| children |
| 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() { |
| 108 | 4 var WebSocket = window.WebSocket || window.MozWebSocket || undefined; |
| 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) { | |
|
100
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
16 this.service = aService; |
| 106 | 17 this.jid = aJid; |
|
100
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
18 }; |
| 108 | 19 WebSocketTransport.prototype = new EventEmitter(); |
| 20 WebSocketTransport.prototype.open = function() { | |
| 21 if(!WebSocket) | |
|
100
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
22 return; //TODO: error |
|
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
23 |
| 106 | 24 this.socket = new WebSocket(this.service, 'xmpp'); |
| 25 | |
|
100
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
26 var that = this; |
|
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
27 this.socket.addEventListener('open', function() { |
| 108 | 28 if (this.protocol !== 'xmpp') |
| 29 ; //TODO: warning (Opera and Safari doesn't support this property) | |
| 30 | |
|
100
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
31 that.emit('open'); |
|
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
32 |
|
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
33 var stream = Lightstring.stanzas.stream.open(that.jid.domain); |
| 106 | 34 var stanza = new Lightstring.Stanza(); |
| 35 stanza.toString = function() { | |
| 36 return stream; | |
| 37 } | |
| 38 that.send(stanza); | |
|
100
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('error', function(e) { |
|
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
41 that.emit('disconnecting', e.data); |
|
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
42 //TODO: error |
|
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
43 }); |
|
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
44 this.socket.addEventListener('close', function(e) { |
|
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
45 that.emit('disconnected', e.data); |
|
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 this.socket.addEventListener('message', function(e) { |
| 106 | 48 var stanza = new Lightstring.Stanza(e.data); |
| 49 that.emit('in', stanza); | |
|
100
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 }; |
| 108 | 52 WebSocketTransport.prototype.send = function(aStanza) { |
| 106 | 53 this.emit('out', aStanza); |
| 54 this.socket.send(aStanza.toString()); | |
|
100
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
55 }; |
| 108 | 56 |
|
100
3e124209821a
move bosh.js and websocket.js to transports/
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
57 })(); |
