I'm developing a server with Node JS where I create multiple childs that open a html with chrome.

app.post('/exeVideo', function(req,res) {
    child = child_process.spawn('chromium-browser', ['RaspMediaVideos.html']);    
    child.on('exit', function() {            
        res.writeHead(200,"0K",{'Content-Type': 'text/plain'});
        res.end(); 
    });
});

This Html has a socket.io communication with the server:

io.on("connection", function ( socket ){

    app.post('/playVideo', function(req,res) {
        socket.emit("playvideo");
        res.writeHead(200,"0K",{'Content-Type': 'text/plain'});
        res.end();          
    });

    app.post('/pauseVideo', function(req,res) {
        socket.emit("pausevideo");
        res.writeHead(200,"0K",{'Content-Type': 'text/plain'});
        res.end();      
    });

My problem here is that if I create 2 childs_process with the code showed above, both chrome windows executes well but only the first one is taking the socket.io events (works good). The second one don't respond to any event :S How can I make it to make ALL childs capture the socket.io events?

Here's the script of html handling events:

var socket = io.connect('http://localhost:3434');

socket.on("pausevideo", function() {
    player.pause();
});

socket.on("playvideo", function() {
    player.play();
});

Thanks for your time.

有帮助吗?

解决方案

You're calling socket.emit in those functions which will only send to the calling socket. Try something like this instead.

app.post('/playVideo', function(req,res) {
    io.sockets.emit("playvideo");
    res.writeHead(200,"0K",{'Content-Type': 'text/plain'});
    res.end();          
});

app.post('/pauseVideo', function(req,res) {
    io.sockets.emit("pausevideo");
    res.writeHead(200,"0K",{'Content-Type': 'text/plain'});
    res.end();      
});


io.on("connection", function (socket) {
    // Handle Events
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top