简体   繁体   中英

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.

//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

//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'));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM