Node.js + Socket.io + Apache

后端 未结 2 844
攒了一身酷
攒了一身酷 2020-12-24 04:04

I\'m looking for a way to integrate Node.js + Socket.io + Apache in the following way: I want apache to continue serving HTML / JS files. I want node.js to listen for connec

2条回答
  •  暖寄归人
    2020-12-24 04:34

    Serve your static content from Apache port 80 and serve your dynamic/data content over a Socket.IO server on port 8080. You don't need the app = require('http').createServer(handler) in your Socket.IO app

    Apache port 80 |-------------| clients |------------| Socket.IO port 8080

    var io = require('socket.io').listen(8080);
    
    io.sockets.on('connection', function (socket) {
      io.sockets.emit('this', { will: 'be received by everyone'});
    
      socket.on('clientMSG', function (from, msg) {
        console.log('I received a private message by ', from, ' saying ', msg);
      });
    
      socket.on('disconnect', function () {
        sockets.emit('user disconnected');
      });
    });
    

提交回复
热议问题