Could not connect to localhost:3000
17:02:53
Error: Unexpected server response: 404
Handshake Details
Request URL: http://localhost:3000/socket.io/?EIO=3&transport=websocket
Request Method: GET
Status Code: 404 Not Found
我的快遞服務器在 3000 埠和 443 埠上的 socketio http 上偵聽,我能夠通過在 ec2 實體上托管它并在郵遞員套接字連接 ui 上使用帶有埠號的 ec2 ip 來連接套接字,但是在 localhost 連接上總是失敗,郵遞員出現上述錯誤套接字測驗版用戶界面。
uj5u.com熱心網友回復:
async function startServer() {
const app = express();
const credentials = {key: key, cert: crt};
const httpsServer = createServer(credentials,app);
const io = socketIO(httpsServer);
await require('./loaders').default({ expressApp: app, socketIO: io });
httpsServer.listen(443);
app.listen(config.port, err => {
if (err) {
Logger.error(err);
process.exit(1);
return;
}
Logger.info(`
################################################
??? Server listening on port: ${config.port} ???
################################################
`);
});
}
這是我的 app.ts 檔案,我使用的是 Ubuntu。所以不允許使用 443 埠,我將此埠更改為 4000,現在應用程式偵聽 3000 和 4000 上的套接字 http 服務器,但是 socketio 無法與套接字 url localhost:3000 連接
uj5u.com熱心網友回復:
您listen只需要呼叫一次。如果你通過你的http/https服務器來表達,app.listen()就足夠了。(除非你想監聽 2 個不同的埠。為了測驗,我們只呼叫一次。)
為了訪問它localhost嘗試用引數顯式地設定hostname在app.listen()。
例子:
async function startServer() {
const app = express();
const credentials = {key: key, cert: crt};
const httpsServer = createServer(credentials,app);
const io = socketIO(httpsServer);
await require('./loaders').default({ expressApp: app, socketIO: io });
// Let's only call listen once for testing purposes. If you call
// listen on the express app, your https server will automatically listen
// to the same configuration.
// httpsServer.listen(4000);
const port = 3000
const hostname = 'localhost'
// explicitly let the app listen on localhost. If hostname is not
// provided, it will take the first found ipv4 interface
app.listen(port, hostname, err => {
if (err) {
Logger.error(err);
process.exit(1);
return;
}
Logger.info(`
################################################
??? Server listening on https://${hostname}:${port} ???
################################################
`);
});
}
現在您應該可以連接您的套接字了。如果它仍然不起作用,請嘗試使用http模塊而不是https更好地隔離您的問題。
請注意,您的服務器現在只能使用localhost而不是通過 ip訪問。只有在運行具有不同主機名的 2 個服務器實體時,這兩種方法才有可能。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/325587.html
標籤:节点.js ssl socket.io 邮差 本地主机
下一篇:什么決定了密碼套件?
