[Node.js] Create a program that receives data from websockets on the Network tab of Chrome developer tools on a specific home page

Asked 2 years ago, Updated 2 years ago, 124 views

Hello.

I'm posting a question like this because I got curious while writing the current program.

If you go to Chrome Developer Tools -> Network tab on a specific homepage, you can find information related to the web socket, so I'm writing a program that receives it as Node.js.


on the General tab of the Header Request URL: wss://(url):(port)/socket.io/?streams=(streams)&EIO=(EIO)&transport=websocket&sid=(sid)
It says so, so I'm writing a program with wss://(url):(port)/ as the url criterion. Also on the Query String Parameters tab, parentheses are provided above streams: (streams)
EIO: (EIO)
transport: websocket
sid: (sid)
Contains values.

Below is the program I wrote.

const socket = require('socket.io-client');

let url = 'wss://(url):(port)';
let io = socket.connect(url);

io.on('connect',(err)=>{
    console.log('Connect!');
    io.send({
        query:{
            'path': '/socket.io',
            'streams': (streams),
            'EIO': (EIO),
            'transport': 'websocket',
            'sid': (sid)
        }
    });
});

io.on('error',(error)=>{
    console.log('err: ',error);
});

io.on('message',(data)=>{
    console.log(data);
});

I thought the contents of the message would come out in io.on('message') because the contents of the message were stacked on the Frames tab on the web socket, but the contents did not come out and only the error kept coming out.

Below is the error content.

err:  { Error: xhr post error
    at XHR.Transport.onError (G:\dev\js\bsx\node_modules\engine.io-client\lib\transport.js:64:13)
    at Request.<anonymous> (G:\dev\js\bsx\node_modules\engine.io-client\lib\transports\polling-xhr.js:109:10)
    at Request.Emitter.emit (G:\dev\js\bsx\node_modules\component-emitter\index.js:133:20)
    at Request.onError (G:\dev\js\bsx\node_modules\engine.io-client\lib\transports\polling-xhr.js:309:8)
    at Timeout._onTimeout (G:\dev\js\bsx\node_modules\engine.io-client\lib\transports\polling-xhr.js:256:18)
    at ontimeout (timers.js:498:11)
    at tryOnTimeout (timers.js:323:5)
    at Timer.listOnTimeout (timers.js:290:5) type: 'TransportError', description: 403 }

I couldn't understand the error because only the contents related to Ajax and jquery came out.

I would really appreciate it if you could tell me what went wrong. ㅠ<

node.js websocket socket.io

2022-09-22 19:57

1 Answers

It's more obvious if you do a real test

There are situations where socket.io can't use the ws, wss protocol right away.

The connection is not made when the server - client mismatch is as shown.

This is shown in the official document . If you look at the topic "What Socket.IO is not" on the link page, you will find out why.

I'm guessing the error was also caused by the connection timeout.

I think you need to use the WebSocket object supported by native feature in your browser to connect.


2022-09-22 19:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.