Javascript SocketIO不在Heroku环境中工作[在本地主机中工作]
我用NodeJS和SocketIO开发了聊天程序。然后我部署到Heroku服务器,发现SocketIO在Heroku中不工作 我已经在Heroku中查看了日志,但是没有任何内容。但奇怪的是,它是在localhost中工作的,而不是在Heroku环境中。我认为端口设置有一些问题 [服务器端代码]Javascript SocketIO不在Heroku环境中工作[在本地主机中工作],javascript,node.js,express,heroku,socket.io,Javascript,Node.js,Express,Heroku,Socket.io,我用NodeJS和SocketIO开发了聊天程序。然后我部署到Heroku服务器,发现SocketIO在Heroku中不工作 我已经在Heroku中查看了日志,但是没有任何内容。但奇怪的是,它是在localhost中工作的,而不是在Heroku环境中。我认为端口设置有一些问题 [服务器端代码] /* Socket IO Settings */ const server = require('http').Server(express); const io = require('socket.io
/* Socket IO Settings */
const server = require('http').Server(express);
const io = require('socket.io')(server);
let port = process.env.PORT || 3001;
server.listen(port, function () {
console.log(`SocketIO Listening on port : ${port}`)
});
/* Chat Functions */
var socket = io.connect(`${window.location.hostname}:3001`);
NODEJS Server : 3000
SOCKET IO : 3001
Heroku process.env.PORT : 3001 (But When I check Heroku console it changes everytime when I reload the dynos and in Logs said me that the ports are already in use.)
[客户端代码]
/* Socket IO Settings */
const server = require('http').Server(express);
const io = require('socket.io')(server);
let port = process.env.PORT || 3001;
server.listen(port, function () {
console.log(`SocketIO Listening on port : ${port}`)
});
/* Chat Functions */
var socket = io.connect(`${window.location.hostname}:3001`);
NODEJS Server : 3000
SOCKET IO : 3001
Heroku process.env.PORT : 3001 (But When I check Heroku console it changes everytime when I reload the dynos and in Logs said me that the ports are already in use.)
第一次我只是使用var socket=io.connect('http://localhost:3001');代码>代码,它在Localhost Env中运行良好。但在我部署到heroku之后,我添加了heroku变量以在生产中使用它
[端口设置]
/* Socket IO Settings */
const server = require('http').Server(express);
const io = require('socket.io')(server);
let port = process.env.PORT || 3001;
server.listen(port, function () {
console.log(`SocketIO Listening on port : ${port}`)
});
/* Chat Functions */
var socket = io.connect(`${window.location.hostname}:3001`);
NODEJS Server : 3000
SOCKET IO : 3001
Heroku process.env.PORT : 3001 (But When I check Heroku console it changes everytime when I reload the dynos and in Logs said me that the ports are already in use.)
我已经阅读了Heroku文档,但似乎我用Express Generator创建了NodeJS应用程序,我认为这应该是不同的解决方案。我已经尝试了heroku文档,但它不起作用在server.listen函数中设置服务器ip
var ip='127.0.0.1';
var port ='3001' ;
server.listen(ip,port, function() {console.log('server stsrt in ip: ' +ip+' and port: ' +port);}) ;
您的问题的可能重复之处在于,heroku dyno未在端口3001上公开,而是在某些随机端口上公开,在端口80上公开。这条线应该能让你到达你需要去的地方。