Has socket io server in java stopped working along with Jetty 11?

ysharma_egain

Yash Sharma

Posted on April 24, 2024

Has socket io server in java stopped working along with Jetty 11?

Based on socket io server documentation
(https://socketio.github.io/engine.io-server-java/using.html#jetty-server) and Jetty 11 APIs, it appears that it is not possible to migrate "socket io bridge jetty" from Jetty 9.0 to Jetty 11.

With latest version of Jetty I am trying to use JettyWebSocketServlet and JettyWebSocketCreator along with JettyWebSocketHandler provided by engine-io-server-jetty. But it is not working.

Following is my piece of code
package com.egain.live.framework.socketIO;

import io.socket.engineio.server.Emitter;
import io.socket.engineio.server.EngineIoServer;
import io.socket.engineio.server.EngineIoServerOptions;
import io.socket.engineio.server.JettyWebSocketHandler;
import io.socket.socketio.server.SocketIoNamespace;
import io.socket.socketio.server.SocketIoServer;
import io.socket.socketio.server.SocketIoSocket;
import org.eclipse.jetty.websocket.core.WebSocketComponents;
import org.eclipse.jetty.websocket.core.server.WebSocketMappings;
import org.eclipse.jetty.websocket.server.JettyServerUpgradeRequest;
import org.eclipse.jetty.websocket.server.JettyServerUpgradeResponse;
import org.eclipse.jetty.websocket.server.JettyWebSocketCreator;
import org.eclipse.jetty.websocket.server.JettyWebSocketServlet;
import org.eclipse.jetty.websocket.server.JettyWebSocketServletFactory;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.Duration;

;

@WebServlet(name = "Socket IO Servlet", urlPatterns = { "/socketiofw/*" })
public class SocketIoServlet extends JettyWebSocketServlet {

private EngineIoServer mEngineIoServer;
private SocketIoServer mSocketIoServer;

@Override
public void configure(JettyWebSocketServletFactory factory) {
    EngineIoServerOptions options = EngineIoServerOptions.newFromDefault();
    options.setAllowSyncPolling(true);
    options.setPingInterval(10000);
    this.mEngineIoServer = new EngineIoServer(options);
    mSocketIoServer = new SocketIoServer(mEngineIoServer);
    factory.setIdleTimeout(Duration.ofSeconds(600L));
    //factory.register(MySocket.class);
    factory.addMapping("/", new MyJettySocketCreator(mEngineIoServer));
}

public SocketIoServlet() {

}

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    mEngineIoServer.handleRequest(request, response);
    SocketIoNamespace namespace = mSocketIoServer.namespace("/");
    mEngineIoServer.on("connection", new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            SocketIoSocket socket = (SocketIoSocket) args[0];
            // Do something with socket

            // Attaching to 'foo' event
            socket.on("foo", new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    System.out.println("Received 'foo' event"+args[0]);
                    socket.send("toClient", args[0]);
                }
            });

            socket.send("toClient", "Hello from socket io server", 1);
        }
    });

    // Broadcasting event 'foo' with args 'bar arg' to room 'room'
    //namespace.broadcast("room", "foo", "bar arg");


}
Enter fullscreen mode Exit fullscreen mode

}

class MyJettySocketCreator implements JettyWebSocketCreator {
EngineIoServer mEngineIoServer;
MyJettySocketCreator(EngineIoServer engineIoServer) {
System.out.println("MyJettySocketCreator constructor");

    mEngineIoServer = engineIoServer;
}

@Override
public Object createWebSocket(JettyServerUpgradeRequest jettyServerUpgradeRequest, JettyServerUpgradeResponse jettyServerUpgradeResponse) {
    return new JettyWebSocketHandler(mEngineIoServer);
}
Enter fullscreen mode Exit fullscreen mode

}

💖 💪 🙅 🚩
ysharma_egain
Yash Sharma

Posted on April 24, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related