Hey,
After much effort, I finally have some code to implement a dynamic chat room using the websocket-multiplex plug in with sockjs and node (First, we activate the new room by going to /activate then we can connect the new mutliplexed websocket).
Everything works swimmingly for the first connexion. The user comes in and when they send some data to the websocket server, it console logs it.
data received: data1
data received: data2
data received: data3
Then when the second connexion comes in, suddenly the first conn.on('data' gets added to the second one and the code for that event gets executed twice for both chat rooms.
data received: data1
data received: data1
data received: data2
data received: data2
data received: data3
data received: data3
It is all duped
How can I correct this?
var express = require('express');
var sockjs = require('sockjs');
var http = require('http');
var multiplex_server = require('websocket-multiplex');
var howManyOpenConns = [];
var sockJsConn = [];
// Sockjs server
var sockjs_opts = {sockjs_url: "http://cdn.sockjs.org/sockjs-0.3.min.js"};
var sockjs_chat = sockjs.createServer(sockjs_opts);
var multiplexer = new multiplex_server.MultiplexServer(sockjs_chat);
// Express server
var app = express();
var server = http.createServer(app);
sockjs_chat.installHandlers(server, {prefix:'/chat'});
console.log(' [*] Listening on 0.0.0.0:8880' );
server.listen(8880,'0.0.0.0');
app.get('/activate', function(req, res) {
console.log('Somebody is trying to activate: ' + req.query.id);
if (isNaN(howManyOpenConns[req.query.id])) {howManyOpenConns[req.query.id]=0;};
console.log('There are currently ' + howManyOpenConns[req.query.id] + ' connections on that object');
if (howManyOpenConns[req.query.id]<1) {
console.log('Open a new redis client');
sockJsConn[req.query.id] = multiplexer.registerChannel('' + req.query.id);
// BEGIN socket connection procedures
sockJsConn[req.query.id].on('connection', function(conn) {
howManyOpenConns[req.query.id] = howManyOpenConns[req.query.id] + 1;
console.log('Increased number of connections for ' + req.query.id + ' to ' + howManyOpenConns[req.query.id]);
console.log('Testing for OpenConns: ' + howManyOpenConns[req.query.id]);
if (howManyOpenConns[req.query.id]<2) {
console.log('This is the first time the socket multiplex opens, let s setup its redis, on data and on close rules');
conn.write('Welcome to sockjs Channel' + req.query.id );
conn.on('data', function(data) {
console.log('data received: ' + data);
// BUG BUG BUG: This event applies to ALL conns for all multiplexed connections while I want it to be dedicated to the one connection!!!
});
conn.on('close', function() {
console.log('closed' + req.query.id);
howManyOpenConns[req.query.id] = howManyOpenConns[req.query.id] -1;
console.log('total connections left: ' + howManyOpenConns[req.query.id]);
});
// END procedures when sockjs connection is closed
}
});
// END OF on socket connection procedures
// BEGIN on socket receives data procedures
//sockJsConn[req.query.id].on('data', function(data) {console.log('does not work');});
}
else {
console.log('I already know you!');
}
res.send('You successfully activated sockJsConn ' + req.query.id);
});
//setInterval(function() {
//},3000);
// new stuff
// eo new stuff