Skip to content
Browse files

Host test_server from sockjs-node, not sockjs-client.

test_server will be expected to run on 8081 by default
  • Loading branch information...
1 parent e39b86f commit 2eebafde73ccfd652958733d4a83c901bf8317cf @majek majek committed
View
1 .gitignore
@@ -1,2 +1,3 @@
+.pidfile.pid
node_modules
lib/*.js
View
39 Makefile
@@ -1,21 +1,42 @@
.PHONY: all serve clean
-all: src/*coffee
+#### General
+
+all: build
+
+build: src/*coffee
@coffee -v > /dev/null
coffee -o lib/ -c src/*.coffee
-serve:
- @while [ 1 ]; do \
- make all; \
- sleep 0.1; \
- inotifywait -r -q -e modify .; \
- done
-
clean:
rm -f lib/*.js
-# Release process:
+#### Testing
+
+test_server:
+ node examples/test_server/server.js
+
+serve:
+ @if [ -e .pidfile.pid ]; then \
+ kill `cat .pidfile.pid`; \
+ rm .pidfile.pid; \
+ fi
+
+ @while [ 1 ]; do \
+ make build; \
+ echo " [*] Running http server"; \
+ make test_server & \
+ SRVPID=$$!; \
+ echo $$SRVPID > .pidfile.pid; \
+ echo " [*] Server pid: $$SRVPID"; \
+ inotifywait -r -q -e modify .; \
+ kill `cat .pidfile.pid`; \
+ rm -f .pidfile.pid; \
+ sleep 0.1; \
+ done
+
+#### Release process
# 1) commit everything
# 2) amend version in package.json
# 3) run 'make tag' and run suggested 'git push' variants
View
2 examples/echo/package.json
@@ -1,6 +1,6 @@
{
"name": "sockjs-echo",
- "version": "0.0.0",
+ "version": "0.0.0-unreleasable",
"dependencies": {
"node-static": "*",
"sockjs": "*"
View
29 examples/test_server/README.md
@@ -0,0 +1,29 @@
+SockJS test server
+==================
+
+In order to test sockjs server implementation the server needs to
+provide a standarized sockjs endpoint that will can used by various
+sockjs-related tests. For example by QUnit or
+[Sockjs-protocol](https://github.com/sockjs/sockjs-protocol) tests.
+
+This small code does exactly that - runs a simple server that supports
+the following SockJS services:
+
+ * `/echo`
+ * `/disabled_websocket_echo`
+ * `/close`
+ * `/ticker`
+ * `/amplify`
+ * `/broadcast`
+
+If you just want to quickly run it:
+
+ npm install
+ node server.js
+
+
+If you want to run do development it's recommended to run `make
+test_server` from the top `sockjs-node` directory:
+
+ cd ../..
+ make test_server
View
8 examples/test_server/config.js
@@ -0,0 +1,8 @@
+exports.config = {
+ sockjs_opts: {
+ sockjs_url: 'http://cdn.sockjs.org/sockjs-0.1.min.js',
+ response_limit: 4096
+ },
+ port: 8081,
+ host: '0.0.0.0'
+};
View
7 examples/test_server/package.json
@@ -0,0 +1,7 @@
+{
+ "name": "sockjs-test-server",
+ "version": "0.0.0-unreleasable",
+ "dependencies": {
+ "sockjs": "*"
+ }
+}
View
19 examples/test_server/server.js
@@ -0,0 +1,19 @@
+var http = require('http');
+var config = require('./config').config;
+var sockjs_app = require('./sockjs_app');
+
+
+var server = http.createServer();
+server.addListener('request', function(req, res) {
+ res.setHeader('content-type', 'text/plain');
+ res.writeHead(404);
+ res.end('404 - Nothing here (via sockjs-node test_server)');
+});
+server.addListener('upgrade', function(req, res){
+ res.end();
+});
+
+sockjs_app.install(config.sockjs_opts, server);
+
+console.log(" [*] Listening on", config.host + ':' + config.port);
+server.listen(config.port, config.host);
View
82 examples/test_server/sockjs_app.js
@@ -0,0 +1,82 @@
+var sockjs = require('sockjs');
+
+exports.install = function(config, server) {
+ var sjs_echo = sockjs.createServer(config.opts);
+ sjs_echo.on('connection', function(conn) {
+ console.log(' [+] echo open ' + conn);
+ conn.on('close', function() {
+ console.log(' [-] echo close ' + conn);
+ });
+ conn.on('data', function(m) {
+ var d = JSON.stringify(m);
+ console.log(' [ ] echo message ' + conn,
+ d.slice(0,64)+
+ ((d.length > 64) ? '...' : ''));
+ conn.write(m);
+ });
+ });
+
+ var sjs_close = sockjs.createServer(config.opts);
+ sjs_close.on('connection', function(conn) {
+ console.log(' [+] clos open ' + conn);
+ conn.close(3000, "Go away!");
+ conn.on('close', function() {
+ console.log(' [-] clos close ' + conn);
+ });
+ });
+
+ var sjs_ticker = sockjs.createServer(config.opts);
+ sjs_ticker.on('connection', function(conn) {
+ console.log(' [+] ticker open ' + conn);
+ var tref;
+ var schedule = function() {
+ conn.write('tick!');
+ tref = setTimeout(schedule, 1000);
+ };
+ tref = setTimeout(schedule, 1000);
+ conn.on('close', function() {
+ clearTimeout(tref);
+ console.log(' [-] ticker close ' + conn);
+ });
+ });
+
+ var broadcast = {};
+ var sjs_broadcast = sockjs.createServer(config.opts);
+ sjs_broadcast.on('connection', function(conn) {
+ console.log(' [+] broadcast open ' + conn);
+ broadcast[conn.id] = conn;
+ conn.on('close', function() {
+ delete broadcast[conn.id];
+ console.log(' [-] broadcast close' + conn);
+ });
+ conn.on('data', function(m) {
+ console.log(' [-] broadcast message', m);
+ for(var id in broadcast) {
+ broadcast[id].write(m);
+ }
+ });
+ });
+
+ var sjs_amplify = sockjs.createServer(config.opts);
+ sjs_amplify.on('connection', function(conn) {
+ console.log(' [+] amp open ' + conn);
+ conn.on('close', function() {
+ console.log(' [-] amp close ' + conn);
+ });
+ conn.on('data', function(m) {
+ var n = Math.floor(Number(m));
+ n = (n > 0 && n < 19) ? n : 1;
+ console.log(' [ ] amp message: 2^' + n);
+ conn.write(Array(Math.pow(2, n)+1).join('x'));
+ });
+ });
+
+
+ sjs_echo.installHandlers(server, {prefix:'[/]echo'});
+ sjs_echo.installHandlers(server, {prefix:'[/]disabled_websocket_echo',
+ disabled_transports: ['websocket']});
+ sjs_close.installHandlers(server, {prefix:'[/]close'});
+ sjs_ticker.installHandlers(server, {prefix:'[/]ticker'});
+ sjs_amplify.installHandlers(server, {prefix:'[/]amplify'});
+ sjs_broadcast.installHandlers(server, {prefix:'[/]broadcast'});
+};

0 comments on commit 2eebafd

Please sign in to comment.
Something went wrong with that request. Please try again.