Skip to content

Instantly share code, notes, and snippets.

@rcastillo
Created May 8, 2012 03:17
Show Gist options
  • Save rcastillo/2632281 to your computer and use it in GitHub Desktop.
Save rcastillo/2632281 to your computer and use it in GitHub Desktop.
socket.io Jasmine helpers for creating test socket.io server and client
var _ = require('underscore'),
SocketIOClient = require('socket.io-client'),
liveSocket = require('../lib/live-socket');
exports.setupClient = function(opts) {
return function() {
opts = opts || {};
_.defaults(opts, {
url: 'http://localhost:8000',
options: {
'reconnect': false,
'transports': ['websocket'],
'force new connection': true
}
});
var socket = SocketIOClient.connect(opts.url, opts.options);
this.clientSockets = this.clientSockets || [];
this.clientSockets.push(socket);
if (!this.clientSocket) {
this.clientSocket = socket;
}
};
};
exports.setupServer = function(port) {
return function() {
this.io = require('socket.io').listen(port || 8000, {
'close timeout': 0.2,
'client store expiration': 0.2,
'log level': 1
});
};
};
exports.registerController = function(name, controller) {
return function() {
this.io.sockets.on('connection', function (socket) {
// server-side: bind the socket to the controllers
liveSocket.configure(name, controller);
// bind the server to the socket
liveSocket.use(socket);
});
};
};
exports.teardownServer = function() {
this.io.server.close();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment