Mercurial > eldonilo > lightstring
diff 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 |
line wrap: on
line diff
new file mode 100644 --- /dev/null +++ b/websocket.js @@ -0,0 +1,43 @@ +'use strict'; + +(function() { + Lightstring.WebSocketConnection = function(aService) { + this.service = aService; + }; + Lightstring.WebSocketConnection.prototype = new EventEmitter(); + Lightstring.WebSocketConnection.prototype.connect = function() { + // Standard + if (typeof(WebSocket) === 'function') + this.socket = new WebSocket(this.service, 'xmpp'); + // Safari + else if (typeof(WebSocket) === 'object') + this.socket = new WebSocket(this.service, 'xmpp'); + // Old Gecko + else if (typeof(MozWebSocket) === 'function') + this.socket = new MozWebSocket(this.service, 'xmpp'); + // No WebSocket support + else + return; //TODO: error + + var Conn = this; + this.socket.addEventListener('open', function() { + //FIXME: Opera/Safari WebSocket implementation doesn't support sub-protocol mechanism. + //if (this.protocol !== 'xmpp') + //return; //TODO: error + Conn.emit('open') + }); + this.socket.addEventListener('error', function(e) { + Conn.emit('disconnecting', e.data); + //TODO: error + }); + this.socket.addEventListener('close', function(e) { + Conn.emit('disconnected', e.data); + }); + this.socket.addEventListener('message', function(e) { + Conn.emit('stanza', e.data); + }); + }, + Lightstring.WebSocketConnection.prototype.send = function(aStanza) { + this.socket.send(aStanza); + } +})(); \ No newline at end of file