trinopoty / socket.io-server-java

Socket.IO Server Library for Java
Other
183 stars 50 forks source link

Continuous Connection request to server from client. #18

Closed mihaialexandruanghel closed 3 years ago

mihaialexandruanghel commented 3 years ago

Hi,

I am using socket.io-server-java on the BE side and socket.io-client on the FE side.

The connection to the socket is working and the communication events between BE and FE are also working. The problem is that after the client is sending multiple requests to the server repeating the connection. I know there are closed issues regarding this but nothing related to spring.

Do you know how can I fix this?

Thanks!

image

Ignore the fact that the request is red. The screenshot is from the server environment.

WebSocketsConfiguration.java

package com.example.demo_from_git.config;

import io.socket.engineio.server.EngineIoServer;
import io.socket.socketio.server.SocketIoServer;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

@EnableAutoConfiguration
@ComponentScan({
        "com.example.demo_from_git.config",
        "com.example.demo_from_git.listener",
        "com.example.demo_from_git.servlet"
})
@ServletComponentScan("com.example.demo_from_git.servlet")
public class WebSocketsConfiguration {

    @Bean
    public EngineIoServer getEngineIoServer() {
        return new EngineIoServer();
    }

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

}

SocketIoServlet.java

package com.example.demo_from_git.servlet;

import com.example.demo_from_git.listener.ConnectionListener;
import io.socket.client.Socket;
import io.socket.engineio.server.EngineIoServer;
import io.socket.socketio.server.SocketIoServer;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletException;

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

@WebServlet("/socket.io/*")
public class SocketIoServlet extends HttpServlet {

    private EngineIoServer engineIoServer;

    private final ConnectionListener connectionListener;

    @Autowired
    public SocketIoServlet(SocketIoServer socketIoServer, EngineIoServer engineIoServer, ConnectionListener connectionListener) {
        this.engineIoServer = engineIoServer;
        this.connectionListener = connectionListener;
        socketIoServer.namespace("/test-socket").on(Socket.EVENT_CONNECT, this.connectionListener);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        engineIoServer.handleRequest(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

ConnectionListener.java

package com.example.demo_from_git.listener;

import io.socket.emitter.Emitter;
import io.socket.socketio.server.SocketIoSocket;
import org.springframework.stereotype.Component;

@Component
public class ConnectionListener implements Emitter.Listener {

    @Override
    public void call(Object... objects) {
        SocketIoSocket socketIoSocket = (SocketIoSocket) objects[0];

        socketIoSocket.send("custom_event", "test socket message");
    }
}

Client.js

const io = require("socket.io-client");

const socket = io.connect('ws://localhost:8080/test-socket')

socket.on("event://get-message", (data) => {
    console.log(data);
});
trinopoty commented 3 years ago

This issue happens when async is disabled and websockets are not working. Please try following these steps: 1) Add the dependency for websockets: “org.springframework:spring-websocket:” and “javax.websocket:javax.websocket-api:1.1” 2) Add a configuration class that extends “WebSocketConfigurer” and has annotation “@EnableWebSocket”. 3) Define beans for “EngineIoServer” and “SocketIoServer” classes 4) Add a “SocketIoHandler” controller class like the one attached. 5) Autowire and use “SocketIoServer” class where you need.

WebsocketConfig.txt SocketIoHandler.txt

mihaialexandruanghel commented 3 years ago

Hi! Thank you for suggestion!

Additionally to my code, I put the classes that you posted above, but nothing changed. Maybe I missed something. Do I still need the SocketIoServlet.java or the SocketIoHandler.java is enough? Because the httpHandler method from the SocketIoHandler.java is not triggered when I have the SocketIoServlet enabled and I do not know where to enable async besides in the @WebServlet annotation.

trinopoty commented 3 years ago

These instructions are for setting up socket.io from scratch. Try removing your existing WebSocketsConfiguration and SocketIoServlet class and then add the classes I mentioned in my previous comment.

mihaialexandruanghel commented 3 years ago

Hi! It worked, now I have fewer requests! Thank you so much!

But I do have another problem now, maybe you had encountered it. Do you know what is causing this? By the way sending the messages from client to server works, but I do not know what to do with this error.

image

mihaialexandruanghel commented 3 years ago

Hi! I managed to get rid of the error. I think I messed something in the handler because after I copied it again it worked. Thank you again for all the support. 👍

mihaialexandruanghel commented 3 years ago

Issue solved!