我是新的 Nodejs socket.io 编程。我试图使用 Flutter Socket 客户端连接套接字。但是当我开始运行节点服务器时(下面的代码)。它打印在端口 3000 上运行的服务器。但是 我收到错误 UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“writeHead”。 它也在最后显示。
UnhandledPromiseRejectionWarning:未处理的承诺拒绝。这个错误要么是因为在没有 catch 块的情况下抛出了异步函数,要么是因为拒绝了一个没有被处理过的承诺
const socketio = require('socket.io');
const express = require('express');
const app = express();
server = app.listen(3000);
//io server
const io = require("socket.io")(server);
//3000 or any other port.
const PORT = 3000 || process.env.PORT;
console.log(`Server running on port ${PORT}`);
var userConnection = [];
io.on('connection', (socket)=>
{
try
{
console.log(`nside connection`);
var other_users = userConnection.filter(p=> p.meeting_id == data.meetingid);
// data saves to userConnection variable
// connection id and socket id are same
userConnection.push({
connectionId: socket.id,
user_id: data.current_user_name,
meeting_id: data.meetingid,
})
}catch(e)
{
console.log(e);
}
})
我得到的错误..
(node:11024) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'writeHead' of undefined
at sendErrorMessage (D:\1ask\9105task\server145\node_modules\engine.io\lib\server.js:568:7)
at Server.handshake (D:\1ask\9105task\server145\node_modules\engine.io\lib\server.js:291:7)
at Server.onWebSocket (D:\1ask\9105task\server145\node_modules\engine.io\lib\server.js:460:12)
at D:\1ask\9105task\server145\node_modules\engine.io\lib\server.js:401:14
at WebSocketServer.completeUpgrade (D:\1ask\9105task\server145\node_modules\ws\lib\websocket-server.js:330:5) at WebSocketServer.handleUpgrade (D:\1ask\9105task\server145\node_modules\ws\lib\websocket-server.js:246:10)
at D:\1ask\9105task\server145\node_modules\engine.io\lib\server.js:400:15
at Server.verify (D:\1ask\9105task\server145\node_modules\engine.io\lib\server.js:172:43)
at Server.handleUpgrade (D:\1ask\9105task\server145\node_modules\engine.io\lib\server.js:384:10)
at Server.<anonymous> (D:\1ask\9105task\server145\node_modules\engine.io\lib\server.js:511:16)
(node:11024) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'writeHead' of undefined
at sendErrorMessage (D:\1ask\9105task\server145\node_modules\engine.io\lib\server.js:568:7)
at Server.handshake (D:\1ask\9105task\server145\node_modules\engine.io\lib\server.js:291:7)
at Server.onWebSocket (D:\1ask\9105task\server145\node_modules\engine.io\lib\server.js:460:12)
at D:\1ask\9105task\server145\node_modules\engine.io\lib\server.js:401:14
at WebSocketServer.completeUpgrade (D:\1ask\9105task\server145\node_modules\ws\lib\websocket-server.js:330:5) at WebSocketServer.handleUpgrade (D:\1ask\9105task\server145\node_modules\ws\lib\websocket-server.js:246:10)
at D:\1ask\9105task\server145\node_modules\engine.io\lib\server.js:400:15
at Server.verify (D:\1ask\9105task\server145\node_modules\engine.io\lib\server.js:172:43)
at Server.handleUpgrade (D:\1ask\9105task\server145\node_modules\engine.io\lib\server.js:384:10)
at Server.<anonymous> (D:\1ask\9105task\server145\node_modules\engine.io\lib\server.js:511:16)