[英]Cant connect to Websocket when app is hosted on Heroku (Node.js)
I tried to deploy my app that uses Websockets to Heroku and I can't connect to the listening port.我尝试将使用 Websockets 的应用程序部署到 Heroku 并且无法连接到侦听端口。
//Server
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8000 });
app.use(express.static(__dirname + '/client/dist'))
app.use(bodyParser.json());
app.listen(port, () => console.log(`Running on port ${port}`));
//Client
this.webSocket = new WebSocket('ws://jitsigame.herokuapp.com:8000');
You don't need to specify the port on the client side.您无需在客户端指定端口。 Heroku provides a detailed guide here: https://devcenter.heroku.com/articles/node-websockets
Heroku 在此处提供详细指南: https://devcenter.heroku.com/articles/node-websockets
//Client
this.webSocket = new WebSocket('ws://jitsigame.herokuapp.com');
You may also want to add on connection handlers server side.您可能还想在服务器端添加连接处理程序。
wss.on('connection', (ws) => {
console.log('Client connected');
ws.on('close', () => console.log('Client disconnected'));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.