my application, using socket.io, cant connect to node.js server.
server node.js
var app = require('http').createServer()
var io = require('socket.io')(app);
app.listen(1000);
io.on('connection', function (client) {
client.name = client.remoteAddress + ':' + client.remotePort;
console.log(client.name + ' connected!');
client.on('sensorChanged', function (data) {
console.log("HERE");
console.log(data);
});
});
android application:
SocketIO socket = new SocketIO();
try {
socket.connect("http://localhost:1000/", this);
txtView.setText("connected");
} catch (MalformedURLException e) {
e.printStackTrace();
}
socket.emit("sensorChanged", "argument1");
When i connect to the server, server doesnt say "socket.name connected", and doesnt emit event 'sensorChanged'. Where is the problem?
I also had connectivity issues when connecting Android app with Socket.IO with my server on the same WiFi (despite the fact that the connection worked when I tried to connect to the same URL in browser).
I found the solution here: https://stackoverflow.com/a/50834600/9560885
In short, try adding this line to your AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:usesCleartextTraffic="true"
...>
...
</application>
</manifest>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With