├── .gitmodules ├── README.md ├── example ├── example-app.js └── test.js └── io-client.js /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "socket.io-node"] 2 | path = socket.io-node 3 | url = http://github.com/LearnBoost/Socket.IO-node.git 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Socket.IO node client 2 | 3 | > Please note that I'm not the author of [Socket.IO](https://github.com/LearnBoost/Socket.IO) - that juicy goodness belongs to LearnBoost 4 | 5 | The `io-client.js` script that allows your client JavaScript that depends on Socket.IO to run in Node. This means that you shuold be able to run your script both on the client side and server side without any modifications. 6 | 7 | # Example 8 | 9 | Where the client side code includes Socket.IO as follows, this *shim* will allow your client code to also run on the server without any modifications (though assuming you're able to import your code as a module in node): 10 | 11 | var socket = new io.Socket('localhost', 8000); 12 | 13 | socket.on('connect', function () { 14 | console.log('yay, connected!'); 15 | socket.send('hi there!'); 16 | }); 17 | 18 | socket.on('message', function (msg) { 19 | console.log('a new message came in: ' + JSON.stringify(msg)); 20 | }); 21 | 22 | socket.connect(); 23 | 24 | If the above code was in `myapp.js`, you can use it in a node app as follows: 25 | 26 | var io = require('./io-client').io; 27 | clientApp = require('./myapp.js'); 28 | 29 | Now `myapp.js` will run without any modifications in a node environment. 30 | 31 | # Note 32 | 33 | I've only tested this shim a little bit and it works perfectly to get [förbind](http://github.com/remy/forbind) connecting from the server side. I've not tested broadcasts or multiple messages or message dropping - maybe there's a solution already that I've not spotted! -------------------------------------------------------------------------------- /example/example-app.js: -------------------------------------------------------------------------------- 1 | // needs to connect to a Socket.IO based server - as you'd do on the client side 2 | var socket = new io.Socket('localhost', 8000); 3 | 4 | socket.on('connect', function () { 5 | console.log('yay, connected!'); 6 | socket.send('hi there!'); 7 | }); 8 | 9 | socket.on('message', function (msg) { 10 | console.log('a new message came in: ' + JSON.stringify(msg)); 11 | }); 12 | 13 | socket.connect(); -------------------------------------------------------------------------------- /example/test.js: -------------------------------------------------------------------------------- 1 | var io = require('../io-client').io; 2 | clientApp = require('./example-app.js'); 3 | 4 | -------------------------------------------------------------------------------- /io-client.js: -------------------------------------------------------------------------------- 1 | (function () { 2 | 3 | var sys = require('sys'), 4 | utils = require('./socket.io-node/lib/socket.io/utils'), 5 | WebSocket = require('./socket.io-node/support/node-websocket-client/lib/websocket').WebSocket, 6 | EventEmitter = require('events').EventEmitter, 7 | io = {}; 8 | 9 | var Socket = io.Socket = function (host, options) { 10 | this.url = 'ws://' + host + ':' + options.port + '/socket.io/websocket'; 11 | this.open = false; 12 | this.sessionId = null; 13 | this._heartbeats = 0; 14 | this.options = { origin: options.origin || 'http://forbind.net' }; 15 | }; 16 | 17 | Socket.prototype = new EventEmitter; 18 | 19 | Socket.prototype.connect = function () { 20 | var self = this; 21 | 22 | function heartBeat() { 23 | self.send('~h~' + ++self._heartbeats); 24 | } 25 | 26 | this.conn = new WebSocket(this.url, 'borf', this.options); 27 | 28 | this.conn.onopen = function () { 29 | self.open = true; 30 | self.emit('connect'); 31 | }; 32 | 33 | this.conn.onmessage = function (event) { 34 | var rawmsg = utils.decode(event.data)[0], 35 | frame = rawmsg.substr(0, 3), 36 | msg; 37 | 38 | switch (frame){ 39 | case '~h~': 40 | return heartBeat(); 41 | case '~j~': 42 | msg = JSON.parse(rawmsg.substr(3)); 43 | break; 44 | } 45 | 46 | if (msg !== undefined) { 47 | self.emit('message', msg); 48 | } 49 | }; 50 | 51 | this.conn.onclose = function () { 52 | self.emit('disconnect'); 53 | self.open = false; 54 | }; 55 | }; 56 | 57 | Socket.prototype.send = function (data) { 58 | if (this.open) { 59 | this.conn.send(utils.encode(data)); 60 | } 61 | }; 62 | 63 | Socket.prototype.disconnect = function () { 64 | if (this.open) { 65 | this.conn.close(); 66 | } 67 | }; 68 | 69 | 70 | this.io = exports.io = io; 71 | 72 | })(); --------------------------------------------------------------------------------