Skip to content

Commit

Permalink
Refactor to use primus.
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriksm committed May 22, 2014
1 parent 3c289be commit 1a96235
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 30 deletions.
4 changes: 2 additions & 2 deletions default.config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
host: 'localhost'
port: 12345
path: 'ws'
port: 3000
auth: 'thisisauth'
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pi-beard",
"version": "0.0.0",
"version": "0.0.1",
"description": "Pi client of something",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -30,7 +30,9 @@
"should": "~3.3.1"
},
"dependencies": {
"engine.io-client": "^1.2.1",
"js-yaml": "~3.0.2",
"ws": "~0.4.31"
"primus": "^2.2.2",
"primus-emitter": "^3.0.2"
}
}
25 changes: 16 additions & 9 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
var http = require('http');
var util = require('util');
var Primus = require('primus');
var Emitter = require('primus-emitter');

var connect = function(config) {
var host = config.host;
var port = config.port;
var path = config.path;
var callback = config.callback;
var WebSocket = require('ws');
var ws = new WebSocket('ws://' + host + ':' + port + '/' + path);
ws.on('open', function() {
var Socket = Primus.createSocket({
transformer: 'engine.io',
plugin: {
'Emitter': require('primus-emitter')
}
});

var client = new Socket(util.format('ws://%s:%d/', host, port));
client.on('open', function() {
console.log('Connected to %s, sending greeting', host);
ws.send('hello');
client.send('auth', config.auth);
if (config.ws) {
config.ws(ws);
config.ws(client);
}
});
ws.on('message', function(data, flags) {
client.on('data', function(data, flags) {
callback(null, data, flags);
});
ws.on('error', function(err) {
client.on('error', function(err) {
callback(err);
});
};
Expand Down
53 changes: 36 additions & 17 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,66 @@
var app = require('../src/app');
var http = require('http');
var should = require('should');
var WebSocketServer = require('ws').Server;
var Primus = require('primus');
var http = require('http');

var response, server;
var config = {
host: 'localhost',
port: 54321
port: 54321,
auth: 'thisisauth'
};

describe('Client part', function() {
it('Should connect to the specified server', function(done) {
var wss = new WebSocketServer({port: config.port});
var server = http.createServer();
var wss = new Primus(server,
{ transformer: 'engine.io' }
);
server.listen(config.port, function() {
app.start(config);
});
wss.on('connection', function(ws) {
ws.on('message', function(message) {
if (message === 'hello') {
wss.close(1234);
ws.on('auth', function(message) {
if (message === config.auth) {
wss.end();
done();
server.close();
}
});
});
app.start(config);
});
var s;
it('Should call callback when sent data', function(done) {
var wss2 = new WebSocketServer({port: config.port});
var server = http.createServer();
var wss2 = new Primus(server,
{ transformer: 'engine.io' }
);
config.port = config.port + 1;
server.listen(config.port, function() {
app.start(config);
});
wss2.on('connection', function(ws) {
s = wss2;
ws.on('message', function(message) {
if (message === 'hello') {
ws.send('world2');
ws.on('auth', function(message) {
if (message === config.auth) {
ws.write('world2');
}
});
});
config.callback = function(err, data) {
should(err).equal(null);
data.should.eql('world2');
data.should.equal('world2');
done(err);
};
app.start(config);
});

it('Should call callback with error', function(done) {
s.close();
var wss3 = new WebSocketServer({port: config.port});
s.end();
var server = http.createServer();
var wss3 = new Primus(server,
{ transformer: 'engine.io' }
);
config.port = config.port + 1;
wss3.on('connection', function(ws) {
});
config.callback = function(err, data) {
Expand All @@ -53,7 +70,9 @@ describe('Client part', function() {
config.ws = function(webss) {
webss.emit('error');
};
app.start(config);
server.listen(config.port, function() {
app.start(config);
});
});

it('Should have an app.log function exposed', function() {
Expand Down

0 comments on commit 1a96235

Please sign in to comment.