套接字io android握手作为客户端失败

时间:2015-05-21 09:36:31

标签: java android node.js multithreading sockets

我试图连接到我的朋友Nodejs套接字服务器。我正在使用这个库:https://github.com/Gottox/socket.io-java-client/

我试图在后台服务中使用它。并使这部分工作。

我也有一个新线程中的套接字(我不知道这是否正确)

代码如下所示:

  @Override
    public void run() {
        SocketIO socket = null;
        try {

            socket = new SocketIO("http://api.speebee.com:3000");

        socket.connect(new IOCallback() {
            @Override
            public void onMessage(JSONObject json, IOAcknowledge ack) {
                try {
                    System.out.println("Server said:" + json.toString(2));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onMessage(String data, IOAcknowledge ack) {
                System.out.println("Server said: " + data);
            }

            @Override
            public void onError(SocketIOException socketIOException) {
                System.out.println("an Error occured");
                socketIOException.printStackTrace();
            }

            @Override
            public void onDisconnect() {
                System.out.println("Connection terminated.");
            }

            @Override
            public void onConnect() {
                System.out.println("Connection established");
            }

            @Override
            public void on(String event, IOAcknowledge ack, Object... args) {
                System.out.println("Server triggered event '" + event + "'");
            }
        });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

这会导致以下错误:

05-21 11:34:23.205    5661-5806/com.geniusgentlemen.gp_android_app 

I/System.out﹕ an Error occured
05-21 11:34:23.205    5661-5806/com.geniusgentlemen.gp_android_app W/System.err﹕ io.socket.SocketIOException: Error while handshaking
05-21 11:34:23.205    5661-5806/com.geniusgentlemen.gp_android_app W/System.err﹕ at io.socket.IOConnection.handshake(IOConnection.java:322)
05-21 11:34:23.205    5661-5806/com.geniusgentlemen.gp_android_app W/System.err﹕ at io.socket.IOConnection.access$7(IOConnection.java:292)
05-21 11:34:23.205    5661-5806/com.geniusgentlemen.gp_android_app W/System.err﹕ at io.socket.IOConnection$ConnectThread.run(IOConnection.java:199)
05-21 11:34:23.205    5661-5806/com.geniusgentlemen.gp_android_app W/System.err﹕ Caused by: java.io.FileNotFoundException: http://api.speebee.com:3000/socket.io/1/
05-21 11:34:23.214    5661-5806/com.geniusgentlemen.gp_android_app W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:206)
05-21 11:34:23.214    5661-5806/com.geniusgentlemen.gp_android_app W/System.err﹕ at io.socket.IOConnection.handshake(IOConnection.java:313)

如果尝试设置默认的ssh但这没有用。我怎样才能让它发挥作用?

1 个答案:

答案 0 :(得分:0)

Socket IO Android Client Example

Socket IO Server Example

检查上述链接,了解包含连接和消息的完整客户端/服务器代码。