php网站有点,哪个网站专门做母婴,谷歌搜索引擎google,各大行业网站前言 我最近一个月一直在寻找能够快速开发实时通讯的简单好用的模块,所以我就去寻找了一下相关的内容.在此之前我使用的是Spring原生的webSocket,她有个弊端就是设置组不容易设置,而且配置上也稍微复杂一点,需要配置拦截器和处理器,还需要把它放入到Springboot的启动容器里面,也…前言 我最近一个月一直在寻找能够快速开发实时通讯的简单好用的模块,所以我就去寻找了一下相关的内容.在此之前我使用的是Spring原生的webSocket,她有个弊端就是设置组不容易设置,而且配置上也稍微复杂一点,需要配置拦截器和处理器,还需要把它放入到Springboot的启动容器里面,也有个好处就是,服务端口是单服务的,而且定制性强,就是很多东西都需要自己来主动配置,让我头疼的就是组的配置 在此之后我就去找寻能让我写的代码最少,而且还易用的服务,就发现了TIO,下面先看一下简介 TIO 极致打磨的底层集群能力可无缝解决IM、物联网等大型产品的集群需求 易学易用让刚毕业的大学生也能轻易驾驭 全方位开箱即用的监控能力 实战中仍表现出卓越的性能不用实验室数据忽悠智慧的大众 内置协议适配能力让多协议接入不再难 内置ack消息能力让RPC等场景轻松实现 自创同步锁、同步安全线程池、同步数据结构等工具库为业务应用提供丰富的开箱即用API 内置半包粘包处理 丰富的生态目前已经用t-io实现了http、websocket、mqtt及大量私有协议 内置慢攻击防御机制帮助应用自动拉黑嫌疑IP 看了上面的话,大概意思就是,简单易学,而且是国人开发的,内部有自动处理半包和粘包处理机制,无需再重新处理粘包,接下来就是Tio的具体配置代码 正文 但是今天我们要用的并不是tio-websocket-server而是另外一个更加简单的服务,不过这个已经在2020年停止更新了,好像是最后一版java1.8版本的,我看了Tio最新版都是用java17的了,如果还在用java1.8的兄弟们可以使用这个依赖tio-websocket-spring-boot-starter,下面是依赖的完整展示 1.POM导入依赖 !--t-io依赖--dependencygroupIdorg.t-io/groupIdartifactIdtio-websocket-spring-boot-starter/artifactIdversion3.6.0.v20200315-RELEASE/version/dependency2.YML配置文件中放入以下配置 # Tio 配置信息
tio:websocket:server:port: 9876#心跳超时时间heartbeat-timeout: 60000#是否支持集群集群开启需要rediscluster:enabled: falseredis:ip: localhostport: 63793.启动类加入注解 注解:EnableTioWebSocketServer 4.创建处理器 package com.xssq.handler;import com.xssq.service.ApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.http.common.HttpRequest;
import org.tio.http.common.HttpResponse;
import org.tio.websocket.common.WsRequest;
import org.tio.websocket.server.handler.IWsMsgHandler;/**
* tio网络套接字味精处理程序
*
* author xssq
* version 1.0.0
* date 2023/09/26
* wisdom 你可以不会但你不能不知道
*/
Component
public class TioWebSocketMsgHandler implements IWsMsgHandler {Autowiredprivate ApiService apiService;/*** 握手** param httpRequest http请求* param httpResponse http响应* param channelContext 渠道上下文* return {link HttpResponse}*/Overridepublic HttpResponse handshake(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) {String token httpRequest.getParam(token);channelContext.setToken(token);Tio.bindGroup(channelContext, 1);Tio.bindUser(channelContext, 1);System.out.println(handshake握手方法);return httpResponse;}/*** 握手后打开** param httpRequest http请求* param httpResponse http响应* param channelContext 渠道上下文*/Overridepublic void onAfterHandshaked(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) {System.out.println(onAfterHandshaked握手后打开的);}/*** 在字节上** param wsRequest ws请求* param bytes 字节* param channelContext 渠道上下文* return {link Object}*/Overridepublic Object onBytes(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) {System.out.println(onBytes方法);return null;}/*** 关闭** param wsRequest ws请求* param bytes 字节* param channelContext 渠道上下文* return {link Object}*/Overridepublic Object onClose(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) {System.out.println(onClose关闭方法);return null;}/*** 在文本上** param wsRequest ws请求* param channelContext 渠道上下文* param message 信息* return {link Object}*/Overridepublic Object onText(WsRequest wsRequest, String message, ChannelContext channelContext) {if (心跳包.equals(message)) {return null;}apiService.sendMsg(channelContext, message);return null;}
}5.创建监听器 package com.xssq.listener;import org.springframework.stereotype.Component;
import org.tio.core.ChannelContext;
import org.tio.core.intf.GroupListener;/**
* spring boot组侦听器
*
* author xssq
* version 1.0.0
* date 2023/10/05
* wisdom 你可以不会但你不能不知道
*/
Component
public class SpringBootGroupListener implements GroupListener {/*** 绑定后打开** param channelContext 信道上下文* param s s*/Overridepublic void onAfterBind(ChannelContext channelContext, String s) {System.out.println(绑定后打开);}/*** 在解除绑定后打开** param channelContext 信道上下文* param s s*/Overridepublic void onAfterUnbind(ChannelContext channelContext, String s) {System.out.println(在解除绑定后打开);}
}6.到此整个教程也就结束了,其实那个监听器也可以不要,不过用于方便绑定组下面是一些绑定组的语法 /*绑定组*/
Tio.bindGroup(channelContext, 1);
/*绑定userId*/
Tio.bindUser(channelContext, 1);
......
...其实还有很多绑定的,还有绑定业务方法,绑定token的,很多,下面是一些服务说明 //最主要的逻辑处理类必须要写否则 抛异常
public class MyWebSocketMsgHandler implements IWsMsgHandler {}
//可不写
public class SpringBootAioListener extends WsServerAioListener {}
//可不写
public class SpringBootGroupListener implements GroupListener {}
//可不写
public class SpringBootIpStatListener implements IpStatListener {}后记 操作完上面的内容就可以正常使用了,感谢大家观看 如果我的博客帮助到了您,您可以到我的博客https://blog.csdn.net/weixin_57228276或者微信公众号搜索幸识SQ,在那里可以找到我,里面也有更多的优秀文章