comparison websocket.js @ 97:11e38a9bfe38

multi-transport is now possible
author Sonny Piers <sonny.piers@gmail.com>
date Tue, 12 Jun 2012 16:35:27 +0200
parents
children 6ec16b3e9cfc
comparison
equal deleted inserted replaced
96:646695bde8e9 97:11e38a9bfe38
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.connect = 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 Conn = 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 Conn.emit('open')
28 });
29 this.socket.addEventListener('error', function(e) {
30 Conn.emit('disconnecting', e.data);
31 //TODO: error
32 });
33 this.socket.addEventListener('close', function(e) {
34 Conn.emit('disconnected', e.data);
35 });
36 this.socket.addEventListener('message', function(e) {
37 Conn.emit('stanza', e.data);
38 });
39 },
40 Lightstring.WebSocketConnection.prototype.send = function(aStanza) {
41 this.socket.send(aStanza);
42 }
43 })();