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)
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
======== **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”}
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
======================================================= i suggest not using the default namespace because V5 will change it https://github.com/socketio/socket.io-protocol#difference-between-v5-and-v4
@mrniko