Skip to content

Commit

Permalink
jsChan in the browser too!
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Aug 20, 2014
1 parent d05e7b8 commit c12c42d
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .zuul.yml
@@ -0,0 +1,5 @@
ui: mocha-bdd
server: ./test/support/browser_server.js
browsers:
- name: chrome
version: latest
14 changes: 14 additions & 0 deletions lib/jschan_browser.js
@@ -0,0 +1,14 @@

'use strict';

// weird hack to work on browserify
// here we have a strange dependency loop
require('stream');
require('readable-stream');

var jschan = {};
module.exports = jschan;

jschan.memorySession = require('./memory/session');
jschan.streamSession = require('./stream/session');
jschan.websocketClientSession = require('./websocket/client');
2 changes: 1 addition & 1 deletion lib/websocket/client.js
Expand Up @@ -9,7 +9,7 @@ function client(url) {
url = 'ws://' + (url.host || 'localhost') + ':' + (url.port || '80');
}

var ws = websocket(url);
var ws = websocket(url, { type: Uint8Array });
var session = jschan.streamSession(ws, ws, { header: false });

return session;
Expand Down
11 changes: 8 additions & 3 deletions package.json
Expand Up @@ -25,7 +25,7 @@
"inherits": "^2.0.1",
"msgpack5": "^1.1.0",
"readable-stream": "^1.0.27-1",
"reduplexer": "0.0.6",
"reduplexer": "0.0.7",
"self-signed": "^1.3.1",
"spdy": "^1.27.0",
"through2": "^0.6.1",
Expand All @@ -35,7 +35,8 @@
"main": "lib/jschan.js",
"scripts": {
"test": "mocha --recursive test 2>&1",
"jshint": "jshint --exclude-path .gitignore ."
"jshint": "jshint --exclude-path .gitignore .",
"test-browser": "zuul --local 8080 -- test/websocket_client.js"
},
"pre-commit": [
"jshint",
Expand All @@ -45,11 +46,15 @@
"type": "git",
"url": "https://github.com/GraftJS/jschan.git"
},
"browser": {
"./lib/jschan.js": "./lib/jschan_browser.js"
},
"devDependencies": {
"concat-stream": "^1.4.6",
"jshint": "^2.5.2",
"mocha": "^1.20.1",
"must": "^0.12.0",
"pre-commit": "0.0.9"
"pre-commit": "0.0.9",
"zuul": "^1.10.1"
}
}
25 changes: 25 additions & 0 deletions test/support/browser_server.js
@@ -0,0 +1,25 @@

'use strict';

var jschan = require('../../lib/jschan.js');
var server = jschan.websocketServer();

function handleMsg(msg) {
var stream = msg.returnChannel;
delete msg.returnChannel;
stream.end(msg);
}

function handleChannel(chan) {
chan.on('data', handleMsg);
}

function handleSession(session) {
session.on('channel', handleChannel);
}

server.on('session', handleSession);

if (process.env.ZUUL_PORT) {
server.listen(process.env.ZUUL_PORT);
}
41 changes: 41 additions & 0 deletions test/websocket_client.js
@@ -0,0 +1,41 @@

'use strict';

if (!process.browser) {
// this is a browser test
return;
}

var jschan = require('../lib/jschan');
var expect = require('must');

describe('websocket client on browserify', function() {
var session;

beforeEach(function() {
session = jschan.websocketClientSession('ws://localhost:8080');
});

afterEach(function(done) {
if (!session) {
return done();
}

session.close(done);
});

it('should work with an echo server', function(done) {
var chan = session.createWriteChannel();
var ret = chan.createReadChannel();

ret.on('data', function(res) {
expect(res).to.eql({ hello: 'world' });
done();
});

chan.end({
hello:'world',
returnChannel: ret
});
});
});

0 comments on commit c12c42d

Please sign in to comment.