Files
socket.io/lib/socket.js
Guillermo Rauch 587c579504 Implementation of buffering for XHR send messages
XHR sending is now handled exclusively by XHR.js
HTMLFile inherits from XHR for POST handling
Implemented heartbeats echoing
Events integrated directly into socket.js
New default method to add events is `on`.
Added aliases for old event handling methods
Simplified utilities / removed legacy code
Updated build.js
Actually implemented the private method _get for XHR transports (we were overriding connect() before)
Updated example/API
2010-07-21 23:16:03 -07:00

125 lines
3.5 KiB
JavaScript

/**
* Socket.IO client
*
* @author Guillermo Rauch <guillermo@learnboost.com>
* @license The MIT license.
* @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
*/
(function(){
var Socket = io.Socket = function(host, options){
this.host = host || document.domain;
this.options = {
secure: false,
document: document,
heartbeatInterval: 4000,
port: document.location.port || 80,
resource: 'socket.io',
transports: ['websocket', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling'],
transportOptions: {},
rememberTransport: true
};
for (var i in options) this.options[i] = options[i];
this.connected = false;
this.connecting = false;
this._events = {};
this.transport = this.getTransport();
if (!this.transport && 'console' in window) console.error('No transport available');
};
Socket.prototype.getTransport = function(){
var transports = this.options.transports, match;
if (this.options.rememberTransport){
match = this.options.document.cookie.match('(?:^|;)\\s*socket\.io=([^;]*)');
if (match) transports = [decodeURIComponent(match[1])];
}
for (var i = 0; transport = transports[i]; i++){
if (io.Transport[transport]
&& io.Transport[transport].check()
&& (!this._isXDomain() || io.Transport[transport].xdomainCheck())){
return new io.Transport[transport](this, this.options.transportOptions[transport] || {});
}
}
return null;
};
Socket.prototype.connect = function(){
if (this.transport && !this.connected && !this.connecting){
this.connecting = true;
this.transport.connect();
}
return this;
};
Socket.prototype.send = function(data){
if (!this.transport || !this.transport.connected) return this._queue(data);
this.transport.send(data);
return this;
};
Socket.prototype.disconnect = function(){
this.transport.disconnect();
return this;
};
Socket.prototype.on = function(name, fn){
if (!(name in this._events)) this._events[name] = [];
this._events[name].push(fn);
return this;
};
Socket.prototype.fire = function(name, args){
if (name in this._events){
for (var i in this._events)
this._events[name][i].apply(this._events[name], args);
}
return this;
};
Server.prototype.removeEvent = function(name, fn){
if (name in this._events){
for (var i in this._events[name]){
for (var a = 0, l = this._events[name].length; a < l; a++)
if (this._events[name][a] == fn) this._events[name].splice(a, 1);
}
}
return this;
};
Socket.prototype._queue = function(message){
if (!('_queueStack' in this)) this._queueStack = [];
this._queueStack.push(message);
return this;
};
Socket.prototype._doQueue = function(){
if (!('_queueStack' in this) || !this._queueStack.length) return this;
this.transport.send(JSON.stringify([].concat(this._queueStack)));
this._queueStack = [];
return this;
};
Socket.prototype._isXDomain = function(){
return this.host !== document.domain;
};
Socket.prototype._onConnect = function(){
this.connected = true;
this.connecting = false;
this._doQueue();
if (this.options.rememberTransport) this.options.document.cookie = 'socket.io=' + encodeURIComponent(this.transport.type);
this.fire('connect');
};
Socket.prototype._onMessage = function(data){
this.fire('message', [data]);
};
Socket.prototype._onDisconnect = function(){
this.fire('disconnect');
};
Socket.prototype.addListener = Socket.prototype.addEvent = Socket.prototype.addEventListener = Socket.prototype.on;
})();