Socket.io + NodeJS 在 Heroku 上不起作用 [英] Socket.io + NodeJS doesn't work on Heroku
问题描述
我正在做一个 API,它在 Heroku 上.但是我只在 heroku 端遇到了 socket.io 的一些问题,当我在本地测试时一切正常.API 完全独立于前端,因此它们位于不同的域(和不同的主机)中.问题是在生产中,我无法成功连接套接字...
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...
我有一些问题,都是关于heroku上的socket.io配置的.我知道有一些帖子提供了一些相关信息,但我发现这些帖子是旧版本的 sockets.io 或旧版本的 heroku(heroku 似乎在去年 7 月改变了 websockets 的东西):
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之前是否需要激活一些东西.我读了一些关于这个的帖子,但似乎都过时了......我试图用:
$ heroku labs:enable websockets
来激活 Websockets,但我得到的响应是:!没有这样的功能: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 websockets
but the response that I got it was:! No such feature: websockets
.
我必须指定一个端口,还是 Heroku 有一个自动端口?
Do I have to specify a port, or Heroku has an automatic port for that?
我需要两个连接吗?一个监听 POST/GET/PUT/DELETE,另一个监听套接字?
Do I need two connections? One to listen the POST/GET/PUT/DELETE and another to the sockets?
app.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
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;
};
}
我再说一遍,一切都在本地主机上运行.我在 localhost:5000 上尝试了 API,在 localhost:80 上尝试了客户端应用程序,所有套接字都可以正常工作.
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.
谢谢.
推荐答案
这里有几件事.如果您想使用 Heroku 的 Websocket 服务(实际上非常棒),您将需要重新编写代码以使用 websockets 的 einaros/ws 实现——然后通过 heroku 命令行添加该服务.https://github.com/einaros/ws/blob/master/doc/ws.md
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
但是,由于您已经将您的应用程序编码到 socket.io,我将简单地重新设计您实例化 socket.io 库的方式:
however, since you've already coded your app to socket.io, I would simply rework how you're instantiating the socket.io library:
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
希望这会有所帮助.
这篇关于Socket.io + NodeJS 在 Heroku 上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!