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

for a practical course at university, I have to write a little game in Java, with a client/server infrastructure. 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). 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? 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.

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

class MyEventListner implements Listener{

@Override

public void call(Object... args) {

System.out.println("myEvent, msg="+args[0].toString());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值