heroku and socket.io不在同一端口上听
因此,我正在Heroku上编码此网络,并尝试将插座集成到设置中。但是问题在于,我不能同时让我的Heroku应用程序和socket.io服务器收听同一端口。
Heroku的默认端口似乎是5000,而Socket.io似乎是3000。
如果我尝试将两个端口更改为与另一个端口相同,则会弹出Eaddrinuse错误。
我不这样
但是,
Listening on 5000
listening on xoxo 3000
如果 实际工作(socket.io),但在localhost上:5000
显然没有。事实上,它在Heroku为我生成的网站上也不起作用(只有Localhost:5000显示),
以下代码线来自我的index.js文件。
我该如何解决?先感谢您。
const express = require('express');
const app = express();
const path = require('path')
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
const PORT = process.env.PORT || 5000;
const INDEX = '/index.ejs'
app.listen(PORT, () => console.log(`Listening on ${ PORT }`))
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.get('/', (req, res) => res.render('index'));
io.on('connection', (socket) => {
socket.on('playerEvent', (msg) => {
console.log(msg);
});
console.log('a user connected');
socket.on('disconnect', () => {
console.log('user disconnected');
});
socket.broadcast.emit('hi');
});
server.listen(3000, () => {
console.log('listening on xoxo 3000');
});
//socket.on('playerEvent', function(msg){
// console.log(msg);
//});
So I am coding this web on Heroku and I try to integrate a socket into the setup. But the problem is that I cannot make both my Heroku app and socket.io server listen to the same port.
The default port for Heroku seems to be 5000 and for socket.io seems to be 3000.
If I try to change either of the ports to the same as the other, then the EADDRINUSE error pops up.
However, if I don't, then the two processes seems to operate separately
when I run npm start
, then the terminal says
Listening on 5000
listening on xoxo 3000
And when I turned on to localhost:3000
it actually works ( the socket.io ), but on localhost:5000
it obviously doesn't. As a matter of fact, it also doesn't work on the Website that Heroku generated for me (only the localhost:5000 showed up)
The code lines below are from my index.js file.
How could I fix this? Thank you in advance.
const express = require('express');
const app = express();
const path = require('path')
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
const PORT = process.env.PORT || 5000;
const INDEX = '/index.ejs'
app.listen(PORT, () => console.log(`Listening on ${ PORT }`))
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.get('/', (req, res) => res.render('index'));
io.on('connection', (socket) => {
socket.on('playerEvent', (msg) => {
console.log(msg);
});
console.log('a user connected');
socket.on('disconnect', () => {
console.log('user disconnected');
});
socket.broadcast.emit('hi');
});
server.listen(3000, () => {
console.log('listening on xoxo 3000');
});
//socket.on('playerEvent', function(msg){
// console.log(msg);
//});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论