我正在使用 Node.js 在 render.com 上创建一个非常简单的 HTTPS 服务器,它升级到 WebSocket 通信。这是代码:
const https = require('https');
// Create an HTTP server
const server = https.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('okay');
});
server.on('upgrade', (req, socket, head) => {
socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
'Upgrade: WebSocket\r\n' +
'Connection: Upgrade\r\n' +
'\r\n');
socket.pipe(socket); // echo back
});
// Now that server is running
server.listen(443, 'test-srv-676.onrender.com', () => {
// make a request
const options = {
port: 443,
Host: 'test-srv-676.onrender.com',
headers: {
'Connection': 'Upgrade',
'Upgrade': 'websocket',
},
};
const req = http.request(options);
req.end();
req.on('upgrade', (res, socket, upgradeHead) => {
console.log('got upgraded!');
socket.end();
});
});
立即,我收到一个错误:服务器尝试侦听从域“test-srv-676.onrender.com”生成的 IP,但它“不可用”。我的 HTTPS 服务器设置可能有问题,但我不知道。抛出的错误是:
Starting service with 'node server.js'
May 22 09:03:18 AM events.js:353
May 22 09:03:18 AM throw er; // Unhandled 'error' event
May 22 09:03:18 AM ^
May 22 09:03:18 AM
May 22 09:03:18 AM Error: listen EADDRNOTAVAIL: address not available 216.24.57.3:443
May 22 09:03:18 AM at Server.setupListenHandle [as _listen2] (net.js:1301:21)
May 22 09:03:18 AM at listenInCluster (net.js:1366:12)
May 22 09:03:18 AM at doListen (net.js:1503:7)
May 22 09:03:18 AM at processTicksAndRejections (internal/process/task_queues.js:83:21)
May 22 09:03:18 AM Emitted 'error' event on Server instance at:
May 22 09:03:18 AM at emitErrorNT (net.js:1345:8)
May 22 09:03:18 AM at processTicksAndRejections (internal/process/task_queues.js:82:21) {
May 22 09:03:18 AM code: 'EADDRNOTAVAIL',
May 22 09:03:18 AM errno: -99,
May 22 09:03:18 AM syscall: 'listen',
May 22 09:03:18 AM address: '216.24.57.3',
May 22 09:03:18 AM port: 443
May 22 09:03:18 AM }