简体   繁体   中英

not connecting in android with socket.io

I'm trying to connect to node.js server in my android app. I can connect to the server on the web only not in my app. It means the server is working properly I guess.

I created the server with express framework. Here is my package.json

{
  "name": "rt",
  "version": "0.0.0",
  "private": true,
  "scripts": {
  "start": "node ./bin/www"
},

"dependencies": {
  "body-parser": "~1.17.1",
  "cookie-parser": "~1.4.3",
  "debug": "~2.6.3",
  "express": "~4.15.2",
  "fs": "0.0.1-security",
  "jade": "~1.11.0",
  "morgan": "~1.8.1",
  "oracledb": "^1.13.1",
  "serve-favicon": "~2.4.2",
  "socket.io": "^1.7.4"

} }

and here is my client code.

private static final String TAG = "MainActivity";

private Socket socket;
{
    try{
        socket = IO.socket("http://210.123.255.155:55555");
    }catch(Exception e){
        Log.e(TAG, e.toString());
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    socket.connect();
    Log.d(TAG, "" + socket.connected());
}

I tried it with 3 libraries.

  1. com.github.nkzawa:socket.io-client:0.3.0
  2. io.socket:socket.io-client:0.8.3
  3. io.socket:engine.io-client:0.8.3

I changed mainActivity code for each libraries, of course.
Three of all are not working. My logcat always says

D/mainActivity: false 

and not any error.

Any ideas for this issue?

Make new class and name it as you like

    public class ChatApplication {
    private Socket mSocket;
    {
        try {
            mSocket = IO.socket(SERVER_URL);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
     }

    public Socket getSocket() {
        return mSocket;
    }
}

in your MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);

    ChatApplication app = new ChatApplication();
    mSocket = app.getSocket();
    mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
    mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
    mSocket.on(Socket.EVENT_CONNECT, onConnect);
    mSocket.on(Socket.EVENT_DISCONNECT, onDisconnect);
    mSocket.connect();
}

public Emitter.Listener onConnect = new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        Log.d(TAG, "Socket Connected!");
    }
};

private Emitter.Listener onConnectError = new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {


            }
        });
    }
};
private Emitter.Listener onDisconnect = new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {


            }
        });
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM