Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
ericz committed Jul 13, 2011
1 parent df4f763 commit c526331
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
10 changes: 7 additions & 3 deletions lib/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* @api public
*/

io.transports = [];
io.transports = ['websocket'];

/**
* Keep track of jsonp callbacks.
Expand Down Expand Up @@ -107,9 +107,9 @@
*/

io.transports.forEach(function (t) {
//io.Transport[t] = require('./transports/node/' + t);
io.Transport[t] = require('./transports/' + t)[t];
});

/**
* Expose Socket
*
Expand Down Expand Up @@ -147,6 +147,7 @@
*/

io.connect = function (host, details) {

var uri = io.util.parseUri(host)
, uuri
, socket;
Expand Down Expand Up @@ -176,8 +177,11 @@

socket = socket || io.sockets[uuri];

var namespace = require('./namespace.js');

// if path is different from '' or /
return socket.of(uri.path.length > 1 ? uri.path : '');
};


})('object' === typeof module ? module.exports : (window.io = {}));
2 changes: 1 addition & 1 deletion lib/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Expose constructor.
*/

exports.SocketNamespace = SocketNamespace;
io.SocketNamespace = SocketNamespace;

/**
* Socket namespace constructor.
Expand Down
43 changes: 19 additions & 24 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
this.options = {
port: 80
, secure: false
, document: document
, document: undefined
, resource: 'socket.io'
, transports: io.transports
, 'connect timeout': 10000
Expand All @@ -51,9 +51,6 @@
(!this.isXDomain() || io.util.ua.hasCORS)) {
var self = this;

io.util.on(window, 'beforeunload', function () {
self.disconnectSync();
}, false);
}

if (this.options['auto connect']) {
Expand Down Expand Up @@ -144,21 +141,23 @@
script.parentNode.removeChild(script);
});
} else {
var xhr = io.util.request();

xhr.open('GET', url);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
xhr.onreadystatechange = empty;

if (xhr.status == 200) {
complete(xhr.responseText);
} else {
!self.reconnecting && self.onError(xhr.responseText);
}
}
var options = {
host: options.host,
port: options.port,
path: ['', this.options.resource
, io.protocol
, '?t=' + + new Date].join('/')
};
xhr.send(null);
require('http').get(options, function(res) {
var body = "";
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function(){
complete(body);
});
});

}
};

Expand All @@ -170,7 +169,6 @@

Socket.prototype.getTransport = function (override) {
var transports = override || this.transports, match;

for (var i = 0, transport; transport = transports[i]; i++) {
if (io.Transport[transport]
&& io.Transport[transport].check(this)
Expand Down Expand Up @@ -209,11 +207,9 @@
function connect (transports){
self.transport = self.getTransport(transports);
if (!self.transport) return self.publish('connect_failed');

self.connecting = true;
self.publish('connecting', self.transport.name);
self.transport.open();

if (self.options['connect timeout']) {
self.connectTimeoutTimer = setTimeout(function () {
if (!self.connected) {
Expand Down Expand Up @@ -331,8 +327,7 @@
*/

Socket.prototype.isXDomain = function () {
var locPort = window.location.port || 80;
return this.options.host !== document.domain || this.options.port != locPort;
return false;
};

/**
Expand Down Expand Up @@ -394,7 +389,7 @@
this.reconnect();
}
}

this.publish('error', err && err.reason ? err.reason : err);
};

Expand Down
4 changes: 3 additions & 1 deletion lib/transports/websocket.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

var WebSocket = require('websocket-client').WebSocket;

/**
* socket.io
* Copyright(c) 2011 LearnBoost <dev@learnboost.com>
Expand Down Expand Up @@ -140,7 +142,7 @@
*/

WS.check = function () {
return 'WebSocket' in window && !('__addTask' in WebSocket);
return true;
};

/**
Expand Down

0 comments on commit c526331

Please sign in to comment.