Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

node.js net websocket receiving data from wrong event

I'm trying to run a websocket server on node.js (just started learning node) and I have this code:

const net = require('net')

const server = net.createServer((socket) => {
    socket.on('text', (data) => {
        console.log(data.toString());
    });
    socket.on('data', (data) => {
    console.log(data.toString());
  });
});

server.listen(9898, () => {
    console.log('Server established on ' + server.address().port);
});

Now, from my phone, I've made an app that connects to the websocket and sends an event of name text. Now, when I emit the event from my phone, not the text event runs, but the data event runs. I get the following log.

GET /socket.io/?EIO=3&transport=polling HTTP/1.1
Host: server2.necrodragon41.repl.co
User-Agent: Dalvik/2.1.0 (Linux; U; Android 7.1.1; LG-M700 Build/NMF26X)
Accept-Encoding: gzip
X-Forwarded-For: 130.211.1.39
X-Forwarded-Proto: https
X-Replit-User-Id: 
X-Replit-User-Name: 
X-Replit-User-Roles:

Now, I cannot make the program run the text function, and print test, that is what I've sent from my phone. Also, the X-Forwarded-For ip always changes. Why is that happening and how I can make the program run the test function instead of the data?

Here is my kotlin code if it works of something.

fun connect() {
        val app: SocketInstance = application as SocketInstance
        var mSocket = app.getMSocket()
//connecting socket
        mSocket?.connect()
        val options = IO.Options()
        options.reconnection = true //reconnection
        options.forceNew = true
        progressBar2.setProgress(100, true)

        if(mSocket?.connected()!!)
        {
            Toast.makeText(this,"Socket is connected",Toast.LENGTH_SHORT).show()
            progressBar2.setProgress(100, true)

            try{
                mSocket.emit("data", "test")
            } catch( e: Exception) {
                Toast.makeText(this,"Error sending",Toast.LENGTH_SHORT).show()
                Thread.sleep(1000)
                progressBar2.setProgress(0, true)
            }
        } else {
            Toast.makeText(this,"Socket is not connected",Toast.LENGTH_SHORT).show()
            Thread.sleep(1000)
            progressBar2.setProgress(100, true)

            try{
                mSocket.emit("text", "test")
                Toast.makeText(this, "Sent", Toast.LENGTH_SHORT).show()
            } catch( e: Exception) {
                Toast.makeText(this, "Error sending", Toast.LENGTH_SHORT).show()
                Thread.sleep(1000)
                progressBar2.setProgress(0, true)
            }
        }
    }

This always outputs not connected, but does not give errors when emitting events.

question from:https://stackoverflow.com/questions/65647542/node-js-net-websocket-receiving-data-from-wrong-event

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...