如何在java中连接socket io nodejs

时间:2015-04-30 07:01:11

标签: java node.js socket.io

当我尝试从java连接nodejs时,我收到此错误:

  

引擎拦截请求路径“/socket.io/”+ 3m   引擎处理“GET”http请求“/socket.io/1/”+ 0ms   引擎未知传输“未定义”+ 0ms

这是我的样本:

package basic;
public class BasicExample implements IOCallback {
    public static void main(String[] args) {
        try {
            new BasicExample();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public BasicExample() throws Exception {
        SocketIO socket = new SocketIO("my nodejs path");
        socket.connect(new IOCallback() {       
            public void onMessage(JSONObject json, IOAcknowledge ack) {

            }       
            public void onMessage(String data, IOAcknowledge ack) {

            }       
            public void onError(SocketIOException socketIOException) {              
            }       
            public void onDisconnect() {                
            }       
            public void onConnect() {               
            }   
            public void on(String event, IOAcknowledge ack, Object... args) {               
            }
        });
        socket.send("Hello Server");
        socket.send(new JSONObject().put("key", "value").put("key2",
                "another value"));
        socket.emit("event", "argument1", "argument2", 13.37);
}
    public void onMessage(JSONObject json, IOAcknowledge ack) {
        try {
            System.out.println("Server said:" + json.toString(2));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

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

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

    public void onDisconnect() {
        System.out.println("Connection terminated.");
    }
    public void onConnect() {
        System.out.println("Connection established");
    }

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

0 个答案:

没有答案