netty-socketio: Server Response: Invalid namespace

issue details in python-socketio

The author of python-socketio told me that it is a server-side issue, but I can’t seem to find the problem. Please help me by combining the link description above and the server-side code example below. Thank you.

`@Slf4j @Service public class SocketIOService {

private final SocketIOServer server;
private final ApplicationEventPublisher eventPublisher;

@Autowired
public SocketIOService(SocketIOServer server, ApplicationEventPublisher eventPublisher) {
    this.server = server;
    this.eventPublisher = eventPublisher;
}

@PostConstruct
public void start() {

    // 为新命名空间添加事件监听器
    server.addConnectListener(this::onConnect);
    server.addDisconnectListener(this::onDisconnect);
    server.addPongListener(this::onPong);
    server.addEventListener("message", String.class, this::onTextMessage);
    server.addEventListener("binary", byte[].class, this::onBinaryMessage);

    server.start();
}


private void onConnect(SocketIOClient client) {
    String token = client.getHandshakeData().getSingleUrlParam("token");
    LoginUser loginUser;
    try {
        loginUser = LoginHelper.getLoginUser(token);
        loginUser.setToken(token);
    } catch (NullPointerException | NotLoginException e) {
        log.error("ws:登录认证失败,握手失败!", e);
        client.disconnect();
        return;
    }
    client.set(LOGIN_USER_KEY, loginUser);
    SocketIoClientHolder.addClient(loginUser.getUserId(), client);
    log.info("[ws-connected] userId: {}", loginUser.getUserId());
    SocketIOUtils.sendMessage(client, "from server: connect-success");
}

private void onDisconnect(SocketIOClient client) {
    LoginUser loginUser = client.get(LOGIN_USER_KEY);
    SocketIoClientHolder.removeClient(loginUser.getUserId());
    log.info("[ws-disconnect] userId: {},", loginUser.getUserId());
}

private void onPong(SocketIOClient client) {
    log.info("ws-pong, userId:" + client.get(LOGIN_USER_KEY));
}

private void onTextMessage(SocketIOClient client, String message, AckRequest ackSender) {
    log.info("ws-text, message:" + message);
    LoginUser loginUser = client.get(LOGIN_USER_KEY);
    eventPublisher.publishEvent(new WebSocketMessageEvent(this, loginUser, message));
}

private void onBinaryMessage(SocketIOClient client, byte[] message, AckRequest ackSender) {
    LoginUser loginUser = (LoginUser) client.get(LOGIN_USER_KEY);
    log.info("ws-binary, userId:" + loginUser.getUserId());

}

@PreDestroy
public void stop() {
    server.stop();
}

} @Slf4j @Configuration public class SocketIOConfig {

@Value("${socketIo.host}")
private String host;

@Value("${socketIo.port}")
private Integer port;

@Value("${socketIo.bossCount}")
private int bossCount;

@Value("${socketIo.workCount}")
private int workCount;

@Value("${socketIo.allowCustomRequests}")
private boolean allowCustomRequests;

@Value("${socketIo.pingTimeout}")
private int pingTimeout;

@Value("${socketIo.pingInterval}")
private int pingInterval;

@Bean
public SocketIOServer socketIOServer() {
    SocketConfig socketConfig = new SocketConfig();
    socketConfig.setTcpNoDelay(true);
    socketConfig.setSoLinger(0);
    com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
    config.setSocketConfig(socketConfig);
    config.setHostname(host);
    config.setPort(port);
    config.setBossThreads(bossCount);
    config.setWorkerThreads(workCount);
    config.setAllowCustomRequests(allowCustomRequests);
    config.setPingTimeout(pingTimeout);
    config.setPingInterval(pingInterval);
    config.setOrigin(null);
    return new SocketIOServer(config);
}

} — # socket.io socketIo: host: 192.168.10.2 port: 8098 bossCount: 1 workCount: 100 allowCustomRequests: true pingTimeout: 20000 pingInterval: 30000`

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 24 (12 by maintainers)

Most upvoted comments

@luoxiaofeng1029

好吧,我发现错误不是 [netty-socketio] 错误,而是 python-socketio 客户端错误,我更改了客户端代码,所有正确的代码图像

另外,我注意到当 python-socketio 成功连接时,它会触发 netty-socketio 中的“connect”事件两次。

@lyjnew

======== **Socket.io协议https://socket.io/docs/v4/socket-io-protocol/#connection-to-a-namespace

1. 客户端发送步骤1:连接打开发送0 --> 服务器返回:0{“sid”:“a4b107b8-daad-4668-806a-3c60aa2ea060”,“upgrades”:[],“pingInterval”:25000," ping超时“:60000}

2.客户端发送第2步:connect open send 0+“namespace” --> 服务器返回:“namespace”+ {“sid”:“f0aa3013-3475-468e-933f-6e4d07bfd7c3”}

图像

您的命名空间设为空值,因此收到的连接请求。看起来像两次相同的连接,

3.如果有命名空间,那就不一样了。服务器还可以完全接收两个不同的连接请求,第一个是 nsp=“”,第二个是 nsp=chat

图像

=================================================== = ===== 我建议不要使用默认命名空间,因为 V5 会更改它 https://github.com/socketio/socket.io-protocol#difference- Between-v5-and-v4 图像

All of my questions have been resolved, thank you for patiently helping me clear all my confusions. I don’t know how to express my gratitude.

@luoxiaofeng1029

ok i find the error not the [netty-socketio] error the python-socketio client error ,i change client code ,all rgiht image

Additionally, I noticed that when python-socketio successfully connects, it triggers the “connect” event in netty-socketio twice.

@lyjnew

======== **Socket.io protocol https://socket.io/docs/v4/socket-io-protocol/#connection-to-a-namespace

1. client send step 1: connect open send 0 --> server return: 0{“sid”:“a4b107b8-daad-4668-806a-3c60aa2ea060”,“upgrades”:[],“pingInterval”:25000,“pingTimeout”:60000}

2. client send step 2: connect open send 0+“namespace” --> server return: “namespace”+ {“sid”:“f0aa3013-3475-468e-933f-6e4d07bfd7c3”}

image

your namespace value is empty value ,so The connection requests your received. look like twice same connect ,

3. If there is a namespace, it would be completely different. The server can also receive two different connection requests, the first with nsp=“”, and the second with nsp=chat

image

======================================================= i suggest not using the default namespace because V5 will change it https://github.com/socketio/socket.io-protocol#difference-between-v5-and-v4 image