【基于t-io聊天集成到springboot中】

基于t-io聊天集成到springboot中


官方文档地址, 点这儿
我是直接下载的demo: https://gitee.com/tywo45/tio-websocket-showcase
大概是这样的:
在这里插入图片描述
这里直接运行main方法就可以了。

集成到spring里面的话可以使用监听器启动t-io的服务,并把serverTioConfig设置为全局变量,便于其他地方使用(比如接口中需要给你某个人发个消息)。


/**
 * @description:初始化聊天相关配置
 * @version:1.0
 */
@Component
@Slf4j
public class InitContextListener implements ApplicationListener<ContextRefreshedEvent> {

    public static ImServerStarter imServerStarter = null;
    public static ServerTioConfig serverTioConfig = null;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        /**先加载配置文件*/
        P.use("app.properties");
        try {
            this.initServer(ShowcaseServerConfig.SERVER_PORT, ShowcaseWsMsgHandler.me);
            this.imServerStarter.start();
            /**
             * 存起来,可全局使用
             */
            ServerConfig.serverTioConfig = this.serverTioConfig;
            ServerConfig.imServerStarter = this.imServerStarter;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @author scw
     */
    public void initServer(int port, ShowcaseWsMsgHandler wsMsgHandler) throws Exception {

        imServerStarter = new ImServerStarter(port, wsMsgHandler);

        serverTioConfig = imServerStarter.getServerTioConfig();
        serverTioConfig.setName(ShowcaseServerConfig.PROTOCOL_NAME);
        serverTioConfig.setServerAioListener(ShowcaseServerAioListener.me);

        //设置ip监控
        serverTioConfig.setIpStatListener(ShowcaseIpStatListener.me);
        //设置ip统计时间段
        serverTioConfig.ipStats.addDurations(ShowcaseServerConfig.IpStatDuration.IPSTAT_DURATIONS);
        //设置心跳超时时间
        serverTioConfig.setHeartbeatTimeout(ShowcaseServerConfig.HEARTBEAT_TIMEOUT);

        if (P.getInt("ws.use.ssl", 1) == 1) {
            //如果你希望通过wss来访问,就加上下面的代码吧,不过首先你得有SSL证书(证书必须和域名相匹配,否则可能访问不了ssl)
//			String keyStoreFile = "classpath:config/ssl/keystore.jks";
//			String trustStoreFile = "classpath:config/ssl/keystore.jks";
//			String keyStorePwd = "214323428310224";
            String keyStoreFile = P.get("ssl.keystore", null);
            String trustStoreFile = P.get("ssl.truststore", null);
            String keyStorePwd = P.get("ssl.pwd", null);
            serverTioConfig.useSsl(keyStoreFile, trustStoreFile, keyStorePwd);
        }
    }

}

ServerConfig


```java
public class ServerConfig {
    public static ImServerStarter imServerStarter = null;
    public static ServerTioConfig serverTioConfig = null;
}

**新手小白,初次接触网络编程,如有不当之处,欢迎指正。**
  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uni-socket.io 是一款适用于多端(包括 Web、小程序、APP 等)的 Socket.io 解决方案,而 SpringBoot 是一款基于 Spring 框架的快速开发框架。如果你想在 SpringBoot 使用 uni-socket.io,可以按照以下步骤进行: 1. 添加依赖:在 pom.xml 文件添加以下依赖: ``` <dependency> <groupId>com.corundumstudio.socketio</groupId> <artifactId>netty-socketio</artifactId> <version>1.7.17</version> </dependency> <dependency> <groupId>com.corundumstudio.socketio</groupId> <artifactId>socketio-spring-boot-starter</artifactId> <version>1.7.17</version> </dependency> ``` 2. 配置 SocketIO 服务器:在 SpringBoot 应用的配置文件 application.properties 添加以下内容: ``` socketio.host=localhost socketio.port=9092 ``` 3. 编写 SocketIO 事件处理程序:编写一个实现了 SocketIOServerAware 接口的类,然后在该类实现所有需要处理的事件。 ``` @Component public class ChatEventHandler implements SocketIOServerAware { private SocketIOServer server; @Autowired private ChatService chatService; @Override public void setSocketIOServer(SocketIOServer server) { this.server = server; } @OnConnect public void onConnect(SocketIOClient client) { // 处理连接事件 } @OnDisconnect public void onDisconnect(SocketIOClient client) { // 处理断开连接事件 } @OnEvent("chat") public void onChat(SocketIOClient client, ChatMessage message) { // 处理聊天事件 } } ``` 4. 启动 SocketIO 服务器:编写一个启动类,然后在该类创建 SocketIOServer 对象并启动服务器。 ``` @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public SocketIOServer socketIOServer() { Configuration config = new Configuration(); config.setHostname("localhost"); config.setPort(9092); SocketIOServer server = new SocketIOServer(config); server.addListeners(new ChatEventHandler()); server.start(); return server; } } ``` 完成上述步骤后,你就可以在 SpringBoot 应用使用 uni-socket.io 进行多端的 Socket.io 开发了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值