Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SocketIOServer with Spring Boot RESTful API on the same port 8080 #1011

Open
radhakrishna67 opened this issue Oct 19, 2024 · 4 comments
Open

Comments

@radhakrishna67
Copy link

I am trying to include netty-socketio in spring boot RESTful API application and am trying to run it on the same port.
I am not successful this approach.
Is it possible to run the both netty-socketio and spring boot on the same port?

@avrilft
Copy link

avrilft commented Oct 20, 2024

@radhakrishna67
Copy link
Author

@avrilft I have to run spring boot restful API along with netty-socketio server in the same apache tomcat on the port 443 in the production. Not like restful api on 443 and netty socketio on 8443 in the production

@avrilft
Copy link

avrilft commented Oct 25, 2024

If you're using springboot, check out the following examples. This example socketio service with springboot embedded tomcat to use the same port, the code below USES the library "https://github.com/trinopoty/socket.io-server-java"
<dependency> <groupId>io.socket</groupId> <artifactId>socket.io-server</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency>xml

`
public class WebSocketConfig extends BaseController {

private final static Logger logger = LoggerFactory.getLogger(WebSocketConfig.class);

@Autowired
private AuthService authService;

@Bean
public ServerEndpointExporter serverEndpointExporter() {
    return new ServerEndpointExporter();
}

@Bean(destroyMethod = "shutdown")
public EngineIoServer engineIoServer() {
    EngineIoServer engine = new EngineIoServer();
    return engine;
}

@Bean
public SocketIoServer socketIoServer() {
    return new SocketIoServer(engineIoServer());
}

@Bean
@Qualifier("/")
public SocketIoNamespace socketIoNamespace() {
    SocketIoNamespace name = socketIoServer().namespace("/");
    return name;
}

}
public class WebSocketController {

private final static Logger logger = LoggerFactory.getLogger(WebSocketController.class);

private final static String USER_PROPERTIES_WEB_SOCKET = "$$USER_PROPERTIES_WEB_SOCKET";

private static EngineIoServer engineIoServer;

@OnOpen
public void onOpen(Session session, EndpointConfig config, @PathParam("token") String token) throws IOException {
    WebSocketIO socket = new WebSocketIO(session);
    session.getUserProperties().put(USER_PROPERTIES_WEB_SOCKET, socket);
    engineIoServer.handleWebSocket(socket);
}

@OnClose
public void onClose(Session session, CloseReason closeReason) {
    WebSocketIO socket = (WebSocketIO) session.getUserProperties().get(USER_PROPERTIES_WEB_SOCKET);
    if (socket != null) {
        socket.close();
    }
}

@OnError
public void onError(Session session, Throwable e) {
    WebSocketIO socket = (WebSocketIO) session.getUserProperties().get(USER_PROPERTIES_WEB_SOCKET);
    if (socket != null) {
        socket.sendError(e);
    }
}

@Autowired
public void setEngineIoServer(EngineIoServer engineIoServer) {
    WebSocketController.engineIoServer = engineIoServer;
}

}`

@nicsor
Copy link
Contributor

nicsor commented Nov 7, 2024

you can do it with some tweaks:

duplicate this class from the library: SocketIOChannelInitializer

and add a custom handler just before PACKET_HANDLER:


=> something like: pipeline.addLast(HTTP_REQUEST_HANDER, httpRequestHandler);

then use that custom class when initializing the socketio object:

        SocketIOServer server = new SocketIOServer(config);
        server.setPipelineFactory(new SocketIOChannelInitializerCustom());

httpRequestHandler mentioned earlier would be an instance of a custom class that implements SimpleChannelInboundHandler
and in which you override channelRead0:

public class HttpRequestHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest fullHttpRequest) throws Exception {
        // look at the path in fullHttpRequest if you want to handle it,
        if (will handle) {
              // add code here to prepare response
                ctx.writeAndFlush(response);
        } else {
            ctx.fireChannelRead(fullHttpRequest.retain());
        }
    }
}

worked for me :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants