spring接入socket.io注意事项

因为前端要求,最后我们采用的socket.io来做我们的消息推送,在联调期间遇到了一些问题,这里记录一下,首先我和前端参考了日常记录-SpringBoot整合netty-socketioVue3和SpringBoot集成SocketIO(WebSocket)

基本流程就是上面这样,然后后端使用的版本是:

java 复制代码
<!-- netty-socketio: 仿`node.js`实现的socket.io服务端 -->
<dependency>
   <groupId>com.corundumstudio.socketio</groupId>
   <artifactId>netty-socketio</artifactId>
   <version>2.0.9</version>
</dependency>

前端使用的版本是

socketio 4.7.5

捕捉不到事件

遇到的问题是:后端启动没问题,前端连接也显示101,可是后端的OnConnect事件完全没动静。

然后前端发出的链接会在域名+端口后面带个socket.io,类似于:

javascript 复制代码
http://localhost:9999/socket.io?userId=12345

通过查看后端的连接,发现的确有一个连接,然后连接的context是socket.io,但是我后端的socket.io上什么事件都没绑定,修改方式是:

handlebars 复制代码
package com.gzgs.socketio.common.config;

import com.corundumstudio.socketio.SocketConfig;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.Optional;

@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.upgradeTimeout}")
    private int upgradeTimeout;

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

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

    @Value("${socketio.namespaces}")
    private String[] namespaces;

    @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.setUpgradeTimeout(upgradeTimeout);
        config.setPingTimeout(pingTimeout);
        config.setPingInterval(pingInterval);
        //注意,这里设定啥,前端的path就要设定啥,如果两边都不设定的话,默认就是socket.io
		config.setContext("/websocket")
        //服务端
        final SocketIOServer server = new SocketIOServer(config);

        //添加命名空间(如果你不需要命名空间,下面的代码可以去掉)
        Optional.ofNullable(namespaces).ifPresent(nss ->
                Arrays.stream(nss).forEach(server::addNamespace));


        return server;
    }

    //这个对象是用来扫描socketio的注解,比如 @OnConnect、@OnEvent
    @Bean
    public SpringAnnotationScanner springAnnotationScanner() {

        return new SpringAnnotationScanner(socketIOServer());
    }
}

收不到消息

第二个问题是,前端获取不到后端推送的消息,这主要是因为我对socket.io的理解不到位,socket.io发送消息一般是用sendEvent,这个方法需要传入event前端才能通过event获取到相应的消息,需要注意一下。

相关推荐
AD钙奶-lalala2 天前
SpringBoot实现WebSocket服务端
spring boot·后端·websocket
wow_DG3 天前
【WebSocket✨】入门之旅(五):WebSocket 的安全性
网络·websocket·网络协议
往事随风去3 天前
别再纠结了!IM场景下WebSocket和MQTT的正确选择姿势,一文讲透!
后端·websocket·架构
郝亚军3 天前
websocket 服务器往客户端发送的数据要加掩码覆盖吗?
服务器·网络·websocket
威斯软科的老司机4 天前
WebSocket压缩传输优化:机器视觉高清流在DCS中的低延迟方案
网络·websocket·网络协议
木头左4 天前
讯飞星火大模型Spark4.0Ultra的WebSocket交互实现解析
websocket·网络协议·交互
paopaokaka_luck4 天前
绿色环保活动平台(AI问答、WebSocket即时通讯、协同过滤算法、Echarts图形化分析)
java·网络·vue.js·spring boot·websocket·网络协议·架构
wow_DG4 天前
【WebSocket✨】入门之旅(三):WebSocket 的实战应用
网络·websocket·网络协议
齐穗穗4 天前
springboot集成websocket
spring boot·后端·websocket
千鼎数字孪生-可视化4 天前
WebSocket实时推送技术:PLC状态监控大屏的高效实现路径
网络·websocket·网络协议