javascript - 当应用程序托管在 Heroku (Node.js) 上时,无法连接到 Websocket
问题描述
我尝试将使用 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');
解决方案
您无需在客户端指定端口。Heroku 在此处提供了详细指南:https ://devcenter.heroku.com/articles/node-websockets
//Client
this.webSocket = new WebSocket('ws://jitsigame.herokuapp.com');
您可能还想在服务器端添加连接处理程序。
wss.on('connection', (ws) => {
console.log('Client connected');
ws.on('close', () => console.log('Client disconnected'));
});
推荐阅读
- ios - 在 Swift 中禁用 UISwitch 时,有什么方法可以不使 UISwitch 变灰/淡出?
- css - 使用 Rmarkdown 制作的海报 CSS 文件:需要字体
- amazon-web-services - 无法访问 AWS Elastic beanstalk EC2 实例
- css - 禁用单个表的 css 样式
- c# - 尝试访问自动完成流时出现 COMException (0x80040102)
- python - 尝试本地运行时出现 SqlAlchemy 问题
- sql - 查询计算每日预算
- java - exoplayer 2.11.* 版本在 android 5.0/5.1(Lollipop) 设备中引发渲染器错误
- javascript - 使用 React 显示隐藏元素
- python - FastAPI:从视图名称(路由名称)中检索 URL