Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

node.js - nodejs socket.io can't receive connection from android Socket

I want my application to use socket.io(nodejs) to communicate with the server. but, my server can't receive connection message. client using kotlin,

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    try {
        mSocket = IO.socket("http://192.168.29.XXX:8080")
        Log.d("TAG", "try")
        mSocket.connect()
    } catch (e: URISyntaxException) {
        Log.d("ERR", e.toString())
    }
}

server code.

var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
app.get('/hi', (req, res) => {
  console.log("user connect22");
  res.send({"result":"hello"});
});

io.on('connection', function(socket) {
  console.log("user connect");

  socket.on('lightOn', function(){
    console.log("light on");
    io.emit('lightOn', "light");
  });

  socket.on('lightOff', function(){
    console.log("light off");
    io.emit('lightOff', "light");
  });
});

server.listen(8080, function(){
  console.log("server on 8080");
});

lient create socket object, but my server's io.on('connection') does not work. I've looked for many ways to solve this problem

  1. add INTERNET permission and android:usesCleartextTraffic="true" to manifest
  2. Asynchronous work with threads
  3. transform dynamic IP to static IP(122.XXX.XXX.XXX)
  4. Turn Off Firewall on my PC
  5. Using "socket.connect" in service

and my server and client do not cause any errors.

question from:https://stackoverflow.com/questions/66056096/nodejs-socket-io-cant-receive-connection-from-android-socket

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

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...