diff --git a/lib/io.js b/lib/io.js index dc5b533e4..fdb50620b 100644 --- a/lib/io.js +++ b/lib/io.js @@ -94,6 +94,14 @@ io.EventEmitter = process.EventEmitter; + /** + * Expose Socket namespace + * + * @api private + */ + + io.SocketNamespace = require('./namespace').SocketNamespace; + /** * Expose Transport * @@ -106,8 +114,9 @@ * Expose all transports */ + require('./transports/websocket'); io.transports.forEach(function (t) { - //io.Transport[t] = require('./transports/node/' + t); + io.Transport[t] = require('./transports/' + t)[t]; }); /** diff --git a/lib/socket.js b/lib/socket.js index 3a6fde680..440bef5d6 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -7,6 +7,17 @@ (function (exports, io) { + /* Stub code for a node.js context */ + if ('object' === typeof module && 'function' === typeof require) { + var document = {}; + var window = {location:{}}; + var http = require('http'), + parseurl = require('url').parse; + var web = false; + } else { + var web = true; + } + /** * Expose constructor. */ @@ -132,7 +143,24 @@ , '?t=' + + new Date ].join('/'); - if (this.isXDomain()) { + if (!web) { + var o = { + host: options.host, + port: options.port, + path: '/' + this.options.resource + '/' + io.protocol + '/' + }; + http.get(o, function(res) { + var data = ''; + res.on('data', function(chunk) { data += chunk; }); + res.on('end', function() { + if (res.statusCode == 200) { + complete(data); + } else { + !self.reconnecting && self.onError(xhr.responseText); + } + }); + }); + } else if (this.isXDomain()) { var insertAt = document.getElementsByTagName('script')[0] , script = document.createElement('SCRIPT'); diff --git a/lib/transports/websocket.js b/lib/transports/websocket.js index b0314736c..9f17737b6 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -7,6 +7,15 @@ (function (exports, io) { + /** + * Add stubs for nodejs websocket-client + */ + + if ('object' === typeof module && 'function' === typeof require) { + var window = require('websocket-client'); + var WebSocket = window.WebSocket; + } + /** * Expose constructor. */ @@ -41,7 +50,7 @@ */ WS.prototype.name = 'websocket'; - + /** * Initializes a new `WebSocket` connection with the Socket.IO server. We attach * all the appropriate listeners to handle the responses from the server. diff --git a/package.json b/package.json index 294bbba8d..4a6bc14da 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ , "url": "https://github.com/LearnBoost/Socket.IO.git" } , "dependencies": { - "uglify-js": "1.0.3" + "uglify-js": "1.0.3", + "websocket-client": "~1" } , "devDependencies": { "expresso": "0.7.7" @@ -25,7 +26,6 @@ , "jade": "0.12.1" , "stylus": "0.13.3" , "socket.io": "0.7.7" - , "socket.io-client": "0.7.4" } , "engines": { "node": ">= 0.4.0" } } diff --git a/test/node/builder.test.js b/test/node/builder.test.js index fef12c84c..6ad4b0ea5 100644 --- a/test/node/builder.test.js +++ b/test/node/builder.test.js @@ -30,7 +30,7 @@ module.exports = { var lines = result.split('\n'); lines.length.should().be.below(5); lines[0].should().match(/production/gi); - Buffer.byteLength(result).should().be.below(41000); + Buffer.byteLength(result).should().be.below(42000); }); },