简体   繁体   English

Java中的SocketIO Client,如何实现与netty-socketio Server一起工作?

[英]SocketIO Client in Java, how to implement working with netty-socketio Server?

for a practical course at university, I have to write a little game in Java, with a client/server infrastructure. 对于大学的实践课程,我必须用Java编写一个带有客户端/服务器基础结构的小游戏。 I need to use Websockets for communication, and other students, whose solutions must be compatible with mine, chose the Netty SocketIO Server for the server-side ( https://github.com/mrniko/netty-socketio/tree/master/src ). 我需要使用Websockets进行通信,其他学生的解决方案必须与我的兼容,为服务器端选择了Netty SocketIO Server( https://github.com/mrniko/netty-socketio/tree/master/src )。 I already know how to set up the server: 我已经知道如何设置服务器:



    Configuration config = new Configuration();
    config.setPort(1234);
    config.setHostname("localhost");

    server = new SocketIOServer(config);

    server.addConnectListener(
        (client) -> {
            System.out.println("Client has Connected!");
    });

    server.addEventListener("MESSAGE", String.class, 
        (client, message, ackRequest) -> {
            System.out.println("Client said: " + message);
    });

    server.start();

Now could you explain to me please, how should the client code look like and which implementation of SocketIOClient (Netty brings only the interface) should I use for it? 现在请你解释一下,客户端代码应该如何,以及我应该使用SocketIOClient(Netty只带来接口)的实现? Would be great if you could show me the code that would produce the output 如果你能告诉我产生输出的代码会很棒

Client has connected!
Client said: any message you'd like :)

I'm really stuck here and was already playing around with implementations like this one https://github.com/socketio/socket.io-client-java but still can't figure out how to build a client and connect to my server. 我真的被困在这里,已经在玩这样的实现https://github.com/socketio/socket.io-client-java但仍然无法弄清楚如何构建客户端并连接到我的服务器。 Thanks for your help. 谢谢你的帮助。

Felix 费利克斯

You can use this 你可以用

Here is sample code. 这是示例代码。

public class MySocketClient {
    public static  void main(String args[]) {
        Options options = new Options();
        options.reconnection= true;
        Socket socket = IO.socket("URL of socket.io server");

        //socket.on

        socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
              @Override
              public void call(Object... args) {
                  System.out.println("Connected");
              }

            })
            .on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
                  @Override
                  public void call(Object... args) {
                      System.out.println("Disonnected");
                  }

            });
        socket.connect();
    }
}

You can have seprate class for event each event. 每个事件都可以为事件分配类。 ie. 即。

socket.on("myEvent", new MyEventListner())

and listner class is 和listner类是

class MyEventListner implements Listener{
    @Override
    public void call(Object... args) {
        System.out.println("myEvent, msg="+args[0].toString());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM