Skip to content

Commit

Permalink
Update example to use more idiomatic sockjs-node api.
Browse files Browse the repository at this point in the history
  • Loading branch information
majek committed Oct 12, 2011
1 parent e72a691 commit 93949d9
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions tests/sockjs_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,69 @@ var sockjs = require('sockjs');

exports.install = function(config, server) {
var sjs_echo = new sockjs.Server(config.opts);
sjs_echo.on('open', function(conn){
sjs_echo.on('connection', function(conn) {
console.log(' [+] echo open ' + conn);
conn.on('close', function(e) {
conn.on('close', function() {
console.log(' [-] echo close ' + conn);
});
conn.on('message', function(e) {
var d = JSON.stringify(e.data);
conn.on('message', function(m) {
var d = JSON.stringify(m);
console.log(' [ ] echo message ' + conn,
d.slice(0,64)+
((d.length > 64) ? '...' : ''));
conn.send(e.data);
conn.send(m);
});
});

var sjs_close = new sockjs.Server(config.opts);
sjs_close.on('open', function(conn){
sjs_close.on('connection', function(conn) {
console.log(' [+] clos open ' + conn);
conn.close(3000, "Go away!");
conn.on('close', function(e) {
conn.on('close', function() {
console.log(' [-] clos close ' + conn);
});
});

var sjs_ticker = new sockjs.Server(config.opts);
sjs_ticker.on('open', function(conn){
sjs_ticker.on('connection', function(conn) {
console.log(' [+] ticker open ' + conn);
var tref;
var schedule = function() {
conn.send('tick!');
tref = setTimeout(schedule, 1000);
};
tref = setTimeout(schedule, 1000);
conn.on('close', function(e) {
conn.on('close', function() {
clearTimeout(tref);
console.log(' [-] ticker close ' + conn);
});
});

var broadcast = {};
var sjs_broadcast = new sockjs.Server(config.opts);
sjs_broadcast.on('open', function(conn){
sjs_broadcast.on('connection', function(conn) {
console.log(' [+] broadcast open ' + conn);
broadcast[conn.id] = conn;
conn.on('close', function(e) {
conn.on('close', function() {
delete broadcast[conn.id];
console.log(' [-] broadcast close' + conn);
});
conn.on('message', function(e) {
console.log(' [-] broadcast message', e);
conn.on('message', function(m) {
console.log(' [-] broadcast message', m);
for(var id in broadcast) {
broadcast[id].send(e.data);
broadcast[id].send(m);
}
});
});

var sjs_amplify = new sockjs.Server(config.opts);
sjs_amplify.on('open', function(conn){
sjs_amplify.on('connection', function(conn) {
console.log(' [+] amp open ' + conn);
conn.on('close', function(e) {
conn.on('close', function() {
console.log(' [-] amp close ' + conn);
});
conn.on('message', function(e) {
var n = Math.floor(Number(e.data));
conn.on('message', function(m) {
var n = Math.floor(Number(m));
n = (n > 0 && n < 19) ? n : 1;
console.log(' [ ] amp message: 2^' + n);
conn.send(Array(Math.pow(2, n)+1).join('x'));
Expand Down

0 comments on commit 93949d9

Please sign in to comment.