Best java server implementation for socket.io

前端 未结 3 2250
北荒
北荒 2020-12-05 02:20

I wanted to use socket.io to push data from server to browser but the project is java tomcat one, and there are many implementation in Github for the server impleme

3条回答
  •  隐瞒了意图╮
    2020-12-05 02:23

    You can try this one: https://github.com/codeminders/socket.io-server-java

    This implementation is loosely based on old Socket.IO-Java library mentioned in other answers.

    It supports Socket.IO 1.0+ clients. The websocket transport is implemented with Jetty 9 but there is no dependency on Jetty for core part of the library. It should not be very difficult to implement websocket transport with Tomcat if needed.

    I tried to keep the API similar to Node.JS Socket.IO server API. So, to send a message to specific socket all you need is to call socket.emit()

    Here is a small code fragment to be called in your SocketIO servlet:

    on(new ConnectionListener() {
            public void onConnect(Socket socket)
            {
                try
                {
                    socket.emit("welcome", "Welcome to Socket.IO Chat!");
                }
                catch (SocketIOException e)
                {
                    socket.disconnect(true);
                }
           }
    }); 
    

提交回复
热议问题