A good solution for a WebSocket / Socket.IO server (I've tried Nginx, Node, etc.)

后端 未结 3 735
一整个雨季
一整个雨季 2020-12-25 09:09

I\'m interested in setting up a Socket.IO server + Rails web application. However, as many are aware, there are not many web servers that support WebSockets. Here have been

3条回答
  •  [愿得一人]
    2020-12-25 09:39

    Here is a node.js socket.io server. note that this only handles websockets.

    var app = require('http').createServer(handler)
      , io = require('socket.io').listen(app)
      , fs = require('fs')
    
    app.listen(8080);
    
    function handler (req, res) {}
    
    io.sockets.on('connection', function (socket) {
      socket.emit('news', { hello: 'world' });
      socket.on('my other event', function (data) {
        console.log(data);
      });
    });
    

    and the corresponding client:

    
    
    

    run node server.js to start up the node server and request the index.html from rails.

    If you need to call rails functions from the websocket server you can communicate over http using either the http node module or this library: https://github.com/mikeal/request

提交回复
热议问题