如何在Java中运行Socket.IO客户端 - 服务器

时间:2016-08-28 17:00:52

标签: java socket.io

我正在尝试在java中运行一个简单的客户端服务器socket.io,但客户端无法建立连接。
我为客户端线程https://github.com/socketio/socket.io-client-java实现了socket.io的实现 在服务器线程上我正在使用https://github.com/mrniko/netty-socketio

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.listener.DataListener;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;

public class BinaryEventLauncher {
    static private Socket socket;
    static final int PORT = 9292;
    static SocketIOServer server;
    public static void main(String[] args) throws InterruptedException {
        Thread ts = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    server();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread tc = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    client();
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        ts.start();
        tc.start();
    }

    public static void server() throws InterruptedException, UnsupportedEncodingException {
        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", "message from server");
            }
        });
        server.start();
        Thread.sleep(Integer.MAX_VALUE);
        server.stop();
    }

    public static void client() throws URISyntaxException, InterruptedException {
        IO.Options opt = new IO.Options();
        opt.port = PORT;
        socket = IO.socket("http://localhost", opt);
        socket.on("toClient", new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                System.out.println(args);
            }
        });
        while(!socket.connected()) {
            socket.connect();
            Thread.sleep(100);
            System.out.println(socket.connected());
        }
        socket.emit("toServer", "test data");//NEVER
     }
}

1 个答案:

答案 0 :(得分:1)

url中的硬代码端口号(不作为选项)。并使用连接事件来捕获建立连接的时间。

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.listener.DataListener;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
public class SocketCS {
    static private Socket socket;
    static final int PORT = 9291;
    static SocketIOServer server;
    public static void main(String[] args) throws InterruptedException {
        Thread ts = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    server();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        });
        ts.start();
        try {
            client();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
    public static void server() throws InterruptedException, UnsupportedEncodingException {
        Configuration config = new Configuration();
        config.setHostname("127.0.0.1");
        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();
        Thread.sleep(10000);
        server.stop();
    }
    public static void client() throws URISyntaxException, InterruptedException {
        socket = IO.socket("http://localhost:" + PORT);
        socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
            @Override
            public void call(Object... objects) {
                socket.emit("toServer", "connected");
                socket.send("test");
            }
        });
        socket.on("toClient", new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                System.out.println("Client recievd : " + args[0]);

            }
        });
        socket.connect();
        while (!socket.connected())
            Thread.sleep(50);
        socket.send("another test");
        Thread.sleep(10000);
        socket.disconnect();
    }
}

控制台输出---&gt;

Client recievd : server recieved connected
Client recievd : message from server test
Client recievd : message from server another test