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
- add INTERNET permission and android:usesCleartextTraffic="true" to manifest
- Asynchronous work with threads
- transform dynamic IP to static IP(122.XXX.XXX.XXX)
- Turn Off Firewall on my PC
- 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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…