Skip to content

Instantly share code, notes, and snippets.

@summer4096
Created December 6, 2012 04:20
Show Gist options
  • Save summer4096/4221749 to your computer and use it in GitHub Desktop.
Save summer4096/4221749 to your computer and use it in GitHub Desktop.
chat.transports.websocket = function(){
this.conn = new WebSocket('ws://web.sock.et/');
var self = this;
this.conn.onmessage = function(ev){
var data = JSON.parse('"'+ev.data+'"');
this.emit('data', data);
};
this.queue = [];
this.conn.onopen = function(){
self.emit('connected');
var packet;
while (packet = self.queue.shift()) {
self.conn.send(packet);
}
};
};
chat.transports.websocket.prototype.write = function(data){
if (this.conn.readyState == 1) {
this.conn.send(packet);
} else {
this.queue.push(packet);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment