Android Studio:Socket.io服务器和客户端

时间:2018-09-22 16:53:30

标签: android server socket.io client netty

我试图在android studio中运行一个简单的client-server socket.io,我基本上是为客户端使用socket.io,并且在服务器线程中使用https://github.com/mrniko/netty-socketio。但是客户端无法连接(但是我成功连接到其他人未在android上实现的另一台服务器)。这是我为客户端和服务器编写的代码:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_class_activity);
    findViewById(R.id.connect_socket).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Thread clientThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    if (mSocket==null || !mSocket.connected()) {
                        try {
                            mSocket = IO.socket("http://127.0.0.1:" + PORT);
                        } catch (URISyntaxException e) {
                            System.out.print(e.getMessage());
                        }

                        if(mSocket!=null) {
                            mSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {

                                @Override
                                public void call(Object... args) {
                                    Log.d("ActivityName: ", "socket connected");
                                    //socket.disconnect();
                                }
                            }).on(Socket.EVENT_MESSAGE, new Emitter.Listener() {

                                @Override
                                public void call(final Object... args) {
                                    Log.d("ActivityName: ", "msg received: " + args[0].toString());
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            ((TextView)findViewById(R.id.response_server)).setText(args[0].toString());
                                        }
                                    });
                                    mSocket.emit(Socket.EVENT_MESSAGE, "salut Sam c est Adrien. J ai recu ton message: \"" + args[0].toString() + "\"");
                                }
                            });
                            mSocket.connect();
                        }
                    }
                }
            });
            clientThread.start();
        }
    });

    findViewById(R.id.start_server).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                server();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    });

private static void server() throws InterruptedException, UnsupportedEncodingException {
    if(server!=null) {
        Configuration config = new Configuration();
        config.setHostname("localhost");
        config.setPort(PORT);
        server = new SocketIOServer(config);
        server.addEventListener("toServer", String.class, new DataListener<String>() {
            @Override
            public void onData(SocketIOClient client, String data, AckRequest ackRequest) {
                client.sendEvent("toClient", "server recieved " + data);
            }
        });
        server.addEventListener("message", String.class, new DataListener<String>() {
            @Override
            public void onData(SocketIOClient client, String data, AckRequest ackRequest) {
                client.sendEvent("toClient", "message from server " + data);
            }
        });
        server.start();
    }
}

}

您知道为什么我的客户端无法连接吗?

1 个答案:

答案 0 :(得分:0)

尝试使用0.0.0.0而不是127.0.0.1,它会在所有接口上接受请求

mSocket = IO.socket("http://0.0.0.0:" + PORT);