websocket koa 网络 套接 字 到 网络 套接 字

3qpi33ja  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(201)

我有一个node.js服务器,它通过一个WebSocket从一个“休眠”的客户端获取数据,然后试图通过它的websocket将数据推送到web客户端。休眠意味着它休眠几分钟,然后醒来进行测量并推送数据,然后再回到休眠状态。见下文:
客户端--〉WebSocket--〉node.js服务器--〉网络套接字--〉网络客户端
只有一个休眠的客户端,但是可以有多个Web客户端。所以当新数据到达时,我需要将其推送到所有Web客户端的Web套接字。为了跟踪这一点,我有一个数组来跟踪Web客户端并存储它们的ctx信息以备后用。

但是,这就是问题所在,当我尝试将数据推送到Web客户端WebSocket时,我遇到了一个上下文错误,node.js崩溃。

下面是我的代码:

var globalCtx = [];
app.ws.use( function( ctx, next ) {
    ctx.websocket.on('message', function ( message )
    {
        if( "telemetry" in jsonMsg )
        {
            console.log( "Got Telemetry: " );
            console.log( message.toString() );
            // we just got a telemetry message,
            // so push it to the all the web client websockets
            globalCtx.forEach( (myCtx, next) => {
                console.log( "DEBUG: ", util.inspect(myCtx) ); <<<-----crashes here
                myCtx.send( jsonMsg );                 <<<----- or crashes here when not debugging
            });
        }
        else
        if( "webClient" in jsonMsg )
        {
            console.log( "Got WS Client: " );
            console.log( message.toString() );

            // we just got a web client connection message
            // so store its context for pushing to later

            if( Array.isArray( globalCtx ) && globalCtx.length )
            {
                // search for an existing entry
                for( let idx = 0; idx < globalCtx.length; idx++ )
                {
                    if( globalCtx[ idx ].ip == ctx.ip )
                    {
                        // we already have this IP stored, do nothing
                        console.log("IP already found: ", ctx.ip );
                        //return next( ctx );
                        return;
                    }
                }

                // we made it here, this means that the IP wasn't found
                console.log("not found, adding IP: ", ctx.ip );
                globalCtx.push( ctx );
            }
            else
            {
                // the array is empty, so just add it
                console.log("empty array adding IP: ", ctx.ip );
                globalCtx.push( ctx );
            }
        }
    });
    return next(ctx);
});

错误如下:

/home/pi/node_modules/koa/lib/response.js:73
    return this.res.statusCode;
                    ^
TypeError: Cannot read property 'statusCode' of undefined
    at Object.get status [as status] (/home/pi/node_modules/koa/lib/response.js:73:21)
at /home/pi/node_modules/only/index.js:6:20
at Array.reduce (<anonymous>)
at module.exports (/home/pi/node_modules/only/index.js:5:15)
at Object.toJSON (/home/pi/node_modules/koa/lib/response.js:                                                                  562:12)
at Object.toJSON (/home/pi/node_modules/koa/lib/context.js:5                                                                  1:31)
at Object.inspect (/home/pi/node_modules/koa/lib/context.js:                                                                  33:17)
at formatValue (internal/util/inspect.js:745:19)
at Object.inspect (internal/util/inspect.js:319:10)
at /home/pi/koaThermostat/index2.js:46:46

下面是myCtx.send( jsonMsg );行的错误

/home/pi/koaThermostat/index2.js:47
                myCtx.send( jsonMsg );
                      ^
TypeError: myCtx.send is not a function
    at /home/pi/koaThermostat/index2.js:47:23
de90aj5v

de90aj5v1#

好吧,我觉得我很傻!当在websockets中使用Koa上下文时,你必须使用websockets对象。所以在我的例子中,它应该是myCtx.websocket.send()而不是myCtx.send()。现在我开始我的下一个bug......

相关问题