尝试在 Heroku 上使用 ws 部署我的应用程序。在我的本地机器上一切正常,但在 Heroku 上部署后,我收到握手错误,响应状态为 200
这是我的服务器代码:
const WebSocketServer = require('ws').Server;
const moment = require('moment');
const app = require('./app');
const wss = new WebSocketServer({ app });
const connections = new Set();
wss.on('connection', (ws) => {
connections.add(ws);
ws.on('message', (message) => {
const time = moment(new Date()).format('HH:mm');
const messageData = {
time,
message,
};
for (const connection of connections) {
connection.send(JSON.stringify(messageData));
}
});
ws.on('close', () => {
connections.delete(ws);
});
});
我的客户代码:
const HOST = location.origin.replace(/^http/, 'ws');
const ws = new WebSocket(HOST);
ws.onmessage = (e) => {
//
};
messageForm.addEventListener('submit', (e) => {
//
ws.send(message);
//
});