Skip to content

Instantly share code, notes, and snippets.

@jasperck
Last active December 14, 2016 06:59
Show Gist options
  • Save jasperck/3ef7261ca2b9eac883928f656e35822a to your computer and use it in GitHub Desktop.
Save jasperck/3ef7261ca2b9eac883928f656e35822a to your computer and use it in GitHub Desktop.
Socket.IO server/client test
class Client {
/**
* constructor
* @param host
* @param port
*/
constructor (host, port) {
this.io = null;
this.socket = null;
this.config = { host, port }
}
/**
* Connect to the server
* @returns {Promise}
*/
connect () {
return new Promise((resolve, reject) => {
this.socket = require('socket.io-client').connect(`http://${this.config.host}:${this.config.port}`);
this.socket.on('connect', () => {
console.log('Client connected');
resolve();
});
this.socket.on('error', reject);
this.socket.on('disconnect', () => console.log('Client disconnected'));
});
}
/**
* Disconnect from the server
*/
disconnect () {
this.socket.disconnect;
}
/**
* Emit event with payload
* @param event
* @param msg
* @returns {Promise}
*/
send (event, payload) {
return new Promise(resolve => this.socket.emit(event, payload, resolve));
}
}
'use strict';
const sinon = require('sinon');
describe('Main test', () => {
const event = 'msg';
const config = { host: 'localhost', port: 8888 };
// Declare server
const server = new Server();
beforeEach('Boot server', (done) => {
server
.on(event, (data, cb) => cb(data))
.boot(config.port);
done();
});
afterEach('Halt server', (done) => {
server.halt();
done();
});
// Declare client
var client = null;
beforeEach('Create client', () => client = new Client(config.host, config.port));
it('should emit event and send payload to server', () => {
const spy = sinon.spy();
const expectation = 'test';
return Promise.resolve()
.then(() => client.connect())
.then(() => server.getSocket().on(event, spy))
.then(() => client.send(event, expectation))
.then(() => sinon.assert.calledWith(spy, expectation));
});
});
class Server {
/**
* Constructor
*/
constructor () {
this.io = null;
this.sockets = [];
this.handlers = {};
}
/**
* Bind customize event
* @param event
* @param handler
* @returns {Server}
*/
on (event, handler) {
this.handlers[event] = handler;
return this;
}
/**
* Boot the server
* @param port
* @returns {Server}
*/
boot (port) {
this.io = require('socket.io')(port);
this.io.on('connection', (socket) => {
this.sockets.push(socket);
// bind event handlers
Object.keys(this.handlers).map(event => socket.on(event, this.handlers[event].bind(this)));
socket.emit('connect');
});
return this;
}
/**
* Halt the server
* graceful shutdown
*/
halt () {
Object.keys(this.sockets).map(socketId => this.sockets[socketId].disconnect());
this.io.close();
}
/**
* Get socket connected on server
* just return it simply for example usage, should do it better for real world needed
* @returns {Socket}
*/
getSocket () {
return this.sockets.pop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment