javascript Socket.io + NodeJS 在 Heroku 上不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25013735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:45:16  来源:igfitidea点击:

Socket.io + NodeJS doesn't work on Heroku

javascriptnode.jsherokuexpresssocket.io

提问by newpatriks

I am doing an API and it's on Heroku. But I am having some problems with the socket.io only on the heroku side, when I test it in local everything goes fine. The API is completely independent of the frontend, so they are in a different domains (and a different hosts). The problem is that on production, I don't get succeed in connect the sockets...

我正在做一个 API,它在 Heroku 上。但是我只在 heroku 端遇到了 socket.io 的一些问题,当我在本地测试时一切正常。API 完全独立于前端,因此它们位于不同的域(和不同的主机)中。问题是在生产中,我没有成功连接套接字......

I have some questions, all of that are about the socket.io configuration on heroku. I know that there are some posts with some information about that, but the posts I found it was with old versions of sockets.io or old versions of heroku (heroku seems to has changed the websockets stuff the past July):

我有一些问题,所有这些都与 heroku 上的 socket.io 配置有关。我知道有一些帖子提供了一些相关信息,但我发现这些帖子是旧版本的 sockets.io 或旧版本的 heroku(heroku 似乎在去年 7 月改变了 websockets 的东西):

  • I don't know if I need to activate something before run socket.io on heroku. I read some posts about that, but all seems to be old... I tried to activate Websockets with: $ heroku labs:enable websocketsbut the response that I got it was: ! No such feature: websockets.

  • Do I have to specify a port, or Heroku has an automatic port for that?

  • Do I need two connections? One to listen the POST/GET/PUT/DELETE and another to the sockets?

  • 我不知道在 heroku 上运行 socket.io 之前是否需要激活某些东西。我读了一些关于这个的帖子,但似乎都过时了......我试图用:激活Websockets:$ heroku labs:enable websockets但我得到的回应是:! No such feature: websockets

  • 我是否必须指定一个端口,或者 Heroku 有一个自动端口?

  • 我需要两个连接吗?一个监听 POST/GET/PUT/DELETE,另一个监听套接字?

app.js

应用程序.js

    var express = require('express');
    var app = module.exports = express();
    var port = process.env.PORT || 5000;
    var port_s = 3000;
    var server = require('http').createServer(express);
    ...
    app.listen(port);
    server.listen(port_s);

    require('./config/socket-io')(app, server, secret);
    app.post('/user', routes.users.register);
    ...

socket-io.js

socket-io.js

module.exports = function(app, server, secret) {
    var clients = {};
    console.log("initiating sockets...");
    var sio = require('socket.io').listen(server);

    sio.sockets.on('connection', function (socket) {
        clients[socket.id] = socket;
        console.log("...new connection: "+socket.client.id);
        socket.emit('identification', { data : socket.client.id });

        socket.on('newShoutOut', function(data) {
            var receptor    = data.idTo;
            var emiter      = socket.client.id;
            //console.log("...new shout out from " +emiter+ " to "+receptor);
            var elem = findElement(sio.sockets['sockets'], 'id', receptor);
            sio.sockets.sockets[elem].emit('privateShoutout',{ data : data.data, from : emiter });
        });

        socket.on('disconnect', function() {
            //console.log("..."+socket.client.id + " disconnected");
        });
    });
};

function findElement(arr, propName, propValue) {
    for (var i=0; i < arr.length; i++) {
        if (arr[i].id === propValue)
            return i;
    };
}

I repeat, everything works on localhost. I tried the API on localhost:5000 and the client app on localhost:80 and all the sockets work fine.

我再说一遍,一切都在本地主机上运行。我在 localhost:5000 上尝试了 API,在 localhost:80 上尝试了客户端应用程序,所有套接字都可以正常工作。

Thank you.

谢谢你。

回答by bencripps

So there's a couple things here. If you'd like to use Heroku's Websocket service (which is pretty great actually), you're going to need to rework your code to use the einaros/ws implementation of websockets--and then add the service via heroku command line. https://github.com/einaros/ws/blob/master/doc/ws.md

所以这里有几件事。如果您想使用 Heroku 的 Websocket 服务(实际上非​​常棒),您将需要重新编写代码以使用 websockets 的 einaros/ws 实现——然后通过 heroku 命令行添加该服务。https://github.com/einaros/ws/blob/master/doc/ws.md

however, since you've already coded your app to socket.io, I would simply rework how you're instantiating the socket.io library:

但是,由于您已经将您的应用程序编码到 socket.io,我将简单地修改您实例化 socket.io 库的方式:

var express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    io = require('socket.io').listen(server),

server.listen(process.env.PORT || 3000);

This should solve your issue, but let me know what the logs show. I think the hang up is that your app, and your socket are running on two different ports.

这应该可以解决您的问题,但请告诉我日志显示的内容。我认为问题在于您的应用程序和套接字在两个不同的端口上运行。

After you've created your sever, you can listen for socket events with:

创建服务器后,您可以使用以下命令侦听套接字事件:

io.sockets.on('connection', function(socket) { //'connection' or any other event

Hope this helps.

希望这可以帮助。