Socket.io + NodeJS在Heroku上不起作用 [英] Socket.io + NodeJS doesn't work on Heroku
问题描述
我有一些问题,所有这些都是关于socket.io配置在英雄上我知道有一些帖子有一些信息,但我发现这是与旧版本的sockets.io或旧版本的heroku(英雄似乎已经改变了websockets的东西过去七月)的帖子:
-
我不知道我是否需要在heroku上运行socket.io之前激活一些东西。我读了一些关于这个的帖子,但是似乎都是老的...我试图用:
$ heroku实验室激活Websockets:启用websockets
但是我得到的响应是:!没有这样的功能:websockets
。 -
我必须指定一个端口,还是Heroku有一个自动端口? p>
-
我需要两个连接吗?一个用于监听POST / GET / PUT / DELETE,另一个用于套接字?
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);
...
套接字io.js p>
module.exports = function(app,server,secret){
var clients = {};
console.log(initiator 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从+ emiter +中喊出来到+受体;
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 +disconnect);
});
});
};
函数findElement(arr,propName,propValue){
for(var i = 0; i< arr.length; i ++){
if(arr [i] id === propValue)
return i;
};
}
我重复一遍,一切都在localhost上运行。我在localhost:5000上尝试了API,在localhost:80上使用了客户端应用程序,所有的套接字工作正常。
谢谢。
所以这里有几件事情。如果您想使用Heroku的Websocket服务(实际上相当不错),您将需要重新编写代码以使用websockets的einaros / ws实现,然后通过heroku命令行添加服务。 https://github.com/einaros/ws/blob/master/doc/ws.md
然而,由于您已经将您的应用程序编码为socket.io,我只会重做如何实例化socket.io库:
var express = require('express'),
app = express(),
server = require('http')。createServer(app),
io = require('socket.io')。listen(server),
server.listen .env.PORT || 3000);
这应该可以解决你的问题,但让我知道日志显示的内容。我认为挂断是你的应用程序,你的套接字在两个不同的端口上运行。
创建服务器后,可以监听套接字事件:
io.sockets.on('connection',function(socket){//'connection'or any other event
希望这有帮助。
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...
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):
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
.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?
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;
};
}
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.
Thank you.
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
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
Hope this helps.
这篇关于Socket.io + NodeJS在Heroku上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!