简体   繁体   中英

Socket.io rooms not working/making sense

I am trying to ensure each individual sees a different message than another using socket.io v1.2.0 (and nodejs 0.12).

I'm basing my work off this answer which doesn't appear to be working (perhaps due to a version difference)? https://stackoverflow.com/a/19150254/1582712

// client side
var socket = io();
var room = new Date().getTime().toString() + "-" + Math.random().toString();
socket.emit('create', room);

// server side
io.sockets.on('connection', function(socket){
    socket.on('create', function(room) {
        socket.join(room);
    });
});

There is more code, of course, but perhaps irrelevant. I will include the full code below. Anyway, my expectation at this point is the client code will (somehow) know it belongs to room room and/or the server knows to emit the proper text to the given room Either way its not working!


// Server
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var id = "0";
var javaSessions = {_counter: -1};

app.get('/', function(req, res){
    res.sendFile(__dirname + '/index.html');
    var javaSessionId = javaSessions._counter++;
    res.cookie('java_session_id', javaSessionId);
    var msnId = req.query.msnId;
    var spawn = require('child_process').spawn;
    var java = spawn("java.exe", 
        ['-cp', 'lib\\*;bin', 'com.app.App', ]);
    javaSessions[javaSessionId] = java;
    var outBuffer = "", errBuffer = "";

    java.stdout.on('data', function(data) {
        outBuffer += data.toString();
        var lines = outBuffer.split("\n");
            for (var i = 0; i < lines.length - 1; i++) {
                var line = lines[i];
                console.log(line);
                io.to(id).emit('stdout', line);
            }
            outBuffer = lines[lines.length - 1];
    });

    java.stderr.on('data', function(data) {
        errBuffer += data.toString();
        var lines = errBuffer.split("\n");
            for (var i = 0; i < lines.length - 1; i++) {
                var line = lines[i];
                console.log("err> " + line);
                io.to(id).emit('stderr', id + " -- " + line);
            }
            errBuffer = lines[lines.length - 1];
    });

    java.stdout.on('end', function() {
        console.log(outBuffer.id);
        io.emit('stdout', outBuffer);
    });

    java.stderr.on('end', function() {
        console.log(errBuffer.id);
        io.emit('stderr', errBuffer);
    });
});

app.get('/unload/:id', function(req, res) {
    var java = javaSessions[req.params.id];
    javaSessions[req.params.id] = null;
    java.kill();
});

io.sockets.on('connection', function(socket){
    socket.on('create', function(room) {
        socket.join(room);
    });

    socket.on('stdout', function(msg){
        io.emit('stdout', msg);
    });

    socket.on('stderr', function(msg){
        io.emit('stderr', msg);
    });
});

http.listen(3000, function(){
    console.log('listening on *:3000');
});


// Client 
var socket = io();
var room = new Date().getTime().toString() + "-" + Math.random().toString();
socket.emit('create', room);

$('form').submit(function(){
    socket.emit('stderr', $('#m').val());
    $('#m').val('');
    return false;
});

socket.on('stdout', function(data) {
    $('#messages').append($('<li class = "stdout">').text(data));
});

socket.on('stderr', function(data) {
    $('#messages').append($('<li class = "stderr">').text(data))
});

Anyways, you might want to use another approach if you only need to send data to 1 client.

Doing something like this

io.emit('stdout', msg);

broadcasts the message to everyone connected. If you only need to send to 1 client then you should do something like

socket.emit('stdout', msg);

So you won't have to do this hakish way of creating a new room for each client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM