Android socket.io app cannot connect to node.js server

my app using socket.io cannot 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 app:

    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, the server doesn't say "socket.name connected" and doesn't emit the "sensorChanged" event. Where is the problem?

+3


source to share


3 answers


Establishing a Socket.IO connection from an Android device to a local Node server


A. Make sure you have installed socket.io-client-java via gradle.


B. Make sure you have enabled Internet access for your device.

<!-- AndroidManifest.xml -->

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="...">

    <uses-permission android:name="android.permission.INTERNET" />

    <application ...>
        ...
    </application>
</manifest>

      


C. Make sure the port on which the server is running is open.


D. Connect to the local server using your computer's IP address.



  • Open a terminal.
  • Enter the command ipconfig

    .
  • Find your IPv4 address.

You want to connect to http://<IPv4 Address>:<port>

.

Connecting to localhost via http://localhost:3000

will not work because "localhost" refers to the device itself.


E. Check the connection on your device.

  • Set up a simple route on the server GET /status

    that just returns 200.
  • Go to http://<IPv4 Address>:<port>/status

    on your device.
  • If you get a successful response, you should be good to go.

F. You can now connect your android socket client to your Node socket server.

// Node.js App

let SocketIO = require('socket.io');

let ioServer = SocketIO(server);
console.log('Socket Server waiting for connections');

ioServer.on('connection', function (socket) {
    console.log(`Socket Client connected with id=${socket.id}`);
});

      

-

// Android App

import io.socket.client.Socket;
import io.socket.client.IO;
import io.socket.emitter.Emitter;
import android.util.Log;

public class SocketIO implements Runnable {
    @Override
    public void run() {

        final Socket socket;

        try {
            socket = IO.socket("http://<IPv4 Address>:<port>");

            socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {

                @Override
                public void call(Object... args) {
                    Log.d("TAG", "Socket Connected!");
                    socket.disconnect();
                }

            });

        } catch(Exception e){
            Log.e("Error", e.toString());
        }

    }
}

      

+1


source


You are targeting the Node server to your local android machine. Since Node doesn't work on Android (as far as I know), I am assuming you want to target a server on your computer. Instead of localhost (which in this Android code will refer to the phone / tablet itself), you should use your computer's network address on your local network.



0


source


Use IP address: 10.0.2.2 for AVD and 10.0.2.3 for genymotion.

If you are using an external device on the same network, use the local IP address of your server.

0


source







All Articles