Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android socket.io application can't connect to node.js server

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?

like image 361
QueUe Avatar asked Aug 31 '14 13:08

QueUe


1 Answers

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>
like image 171
Luka Kralj Avatar answered Sep 27 '22 20:09

Luka Kralj