简体   繁体   English

当应用程序托管在 Heroku (Node.js) 上时,无法连接到 Websocket

[英]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.

相关问题 部署 NODE.JS + MONGODB(CRUD APP)时在 HEROKU 上连接 ECONNREFUSED 127.0.0.1:3000 - connect ECONNREFUSED 127.0.0.1:3000 on HEROKU when deploying NODE.JS + MONGODB (CRUD APP) 将Node.js应用程序部署到Heroku时出现“ ReferenceError:未定义压缩” - “ReferenceError: compression is not defined” when deploying Node.js app to Heroku 推送到Heroku时无法构建Node.js应用 - Node.js app failed to build when pushing to Heroku 在 Heroku 上部署 Node.js 应用程序时出现 403 错误 - 403 error when deploying a Node.js app on Heroku 使用Heroku部署Node.js应用程序时公共目录的问题 - Problems with public directory when deploying Node.js app with Heroku 部署 Node.js/React 应用程序时 Heroku 构建失败 - Heroku build failure when deploying Node.js/React app 当我关闭cmd时,Heroku中的Node.js应用程序崩溃 - Node.js app in Heroku crashes when I close the cmd 将Firebase Firestore连接到Heroku上托管的node.js服务器时,环境变量返回未定义 - Environment variables returning undefined when connecting Firebase Firestore to a node.js server hosted on Heroku Heroku Node.js(express.js)应用程序在本地运行,但在使用MongoDB时在heroku上失败 - Heroku Node.js (express.js) App Works Locally but failed on heroku when using MongoDB 在heroku上遇到node.js应用程序时遇到问题 - Having trouble with a node.js app on heroku
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM