Java套接字未知传输未定义

时间:2017-05-16 15:37:46

标签: javascript java node.js sockets socket.io

当我尝试从java socket.io客户端连接node.js时,我收到此错误:

engine intercepting request for path "/socket.io/" +0ms
engine handling "GET" http request "/socket.io/1/" +0ms
engine unknown transport "undefined" +0ms

同时,当我从javascript客户端尝试时,工作正常:

engine intercepting request for path "/socket.io/" +0ms
engine handling "GET" http request "/socket.io/?EIO=3&transport=polling&t=1494940689150-0&b64=1" +0ms
engine handshaking client "3EFWO3PTlnvZksM8AAAA" +15ms

我的Java客户端代码:

import io.socket.IOAcknowledge;
import io.socket.IOCallback;
import io.socket.SocketIO;
import io.socket.SocketIOException;

import org.json.JSONException;
import org.json.JSONObject;

public class BasicExample implements IOCallback {
    private SocketIO socket;

    public static void main(String[] args) {
        try {
            new BasicExample();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public BasicExample() throws Exception {

            socket = new SocketIO();
            socket.connect("http://localhost:9990", this);

            socket.send("Hello Server");

            // Sends a JSON object to the server.
            socket.send(new JSONObject().put("key", "value").put("key2","another value"));

            // Emits an event to the server.
            socket.emit("event", "argument1", "argument2", 13.37);
    }

    @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 + "'");
    }
}

在Java中,我使用从http://www.java2s.com/Code/Jar/s/Downloadsocketiojar.htm下载的socketio.jar,这似乎是从这里编译的https://github.com/Gottox/socket.io-java-client

在node.js服务器中,我是用户socket.io@1.0.6版本。

拜托,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

这个客户看起来已经过时了5年。这似乎是一个较新的:https://github.com/socketio/socket.io-client-java