为什么 运行 两个具有不同路径的 websocket 服务器导致客户端获得 "invalid frame header" 并断开连接?
Why does running two websocket servers with different paths cause the client to get an "invalid frame header" and disconnect?
这是服务器代码
import path from 'path';
import http from 'http';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import express from 'express';
import WebSocket from 'ws';
const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();
console.log(__dirname)
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + '/v3client/html/botkill.html'));
})
let httpServer = http.createServer(app).listen(80, () => {
console.log('listening');
});
global.hServer = httpServer;
let wss = new WebSocket.Server({ server: global.hServer, path: '/based' });
wss.on('listening', () => {
console.log(`listening with path /based`);
});
function heartbeat() {
this.isAlive = true;
}
wss.on('connection', (socket) => {
console.log('connected')
socket.isAlive = true;
socket.on('pong', heartbeat);
socket.on('close', () => {
console.log('disconnected')
});
});
const pingPong = setInterval(() => {
wss.clients.forEach((ws) => {
if (ws.isAlive === false) return ws.terminate();
ws.isAlive = false;
ws.ping();
});
}, 30000);
new WebSocket.Server({ server: global.hServer, path: '/notsobased' });
这是botkill.html
<script>
let ws = new WebSocket('ws://localhost/based');
ws.onopen = () => {
console.log('open');
};
ws.onclose = async () => {
console.log('disconnected');
};
ws.onerror = async (e) => {
console.log(e, 'error');
};
ws.onmessage = function (e) {
return false;
};
</script>
当我打开网页时它立即断开连接,但是当我注释掉服务器代码的最后一行时它给我错误“无效的帧头”。这是我能做的最小的例子,但我最近在尝试在不同路径上使用两个单独的 websocket 服务器时了解到这一点,这似乎不起作用,但我似乎无法弄清楚为什么。此外,它仍然会立即与 https 断开连接,但它甚至不会在客户端出现错误。
因此,通过搜索相关主题,我了解到问题在于 websocket 服务器都附加 到 http 服务器,而不是 http 服务器本身处理连接到 websocket 服务器。
通过将 http.Server.on('upgrade', ...)
和 Websocket.Server.handleUpgrade
与两台服务器一起使用,我让它工作了。抱歉给您带来不便!
这是服务器代码
import path from 'path';
import http from 'http';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import express from 'express';
import WebSocket from 'ws';
const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();
console.log(__dirname)
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + '/v3client/html/botkill.html'));
})
let httpServer = http.createServer(app).listen(80, () => {
console.log('listening');
});
global.hServer = httpServer;
let wss = new WebSocket.Server({ server: global.hServer, path: '/based' });
wss.on('listening', () => {
console.log(`listening with path /based`);
});
function heartbeat() {
this.isAlive = true;
}
wss.on('connection', (socket) => {
console.log('connected')
socket.isAlive = true;
socket.on('pong', heartbeat);
socket.on('close', () => {
console.log('disconnected')
});
});
const pingPong = setInterval(() => {
wss.clients.forEach((ws) => {
if (ws.isAlive === false) return ws.terminate();
ws.isAlive = false;
ws.ping();
});
}, 30000);
new WebSocket.Server({ server: global.hServer, path: '/notsobased' });
这是botkill.html
<script>
let ws = new WebSocket('ws://localhost/based');
ws.onopen = () => {
console.log('open');
};
ws.onclose = async () => {
console.log('disconnected');
};
ws.onerror = async (e) => {
console.log(e, 'error');
};
ws.onmessage = function (e) {
return false;
};
</script>
当我打开网页时它立即断开连接,但是当我注释掉服务器代码的最后一行时它给我错误“无效的帧头”。这是我能做的最小的例子,但我最近在尝试在不同路径上使用两个单独的 websocket 服务器时了解到这一点,这似乎不起作用,但我似乎无法弄清楚为什么。此外,它仍然会立即与 https 断开连接,但它甚至不会在客户端出现错误。
因此,通过搜索相关主题,我了解到问题在于 websocket 服务器都附加 到 http 服务器,而不是 http 服务器本身处理连接到 websocket 服务器。
通过将 http.Server.on('upgrade', ...)
和 Websocket.Server.handleUpgrade
与两台服务器一起使用,我让它工作了。抱歉给您带来不便!