当前位置: 首页 > wzjs >正文

深圳网站建设补助广州最新疫情情况

深圳网站建设补助,广州最新疫情情况,jsp的网站,wordpress 无限下拉菜单Tio-Boot 集成 Spring Boot 实现即时通讯功能全解析(详细版) 一、Tio-Boot 简介 Tio-Boot 是基于 Tio 框架的 Spring Boot Starter 扩展,提供高性能、低延迟的网络通信能力,支持 TCP/UDP 协议及 WebSocket 协议,适用…

Tio-Boot 集成 Spring Boot 实现即时通讯功能全解析(详细版)

一、Tio-Boot 简介

Tio-Boot 是基于 Tio 框架的 Spring Boot Starter 扩展,提供高性能、低延迟的网络通信能力,支持 TCP/UDP 协议及 WebSocket 协议,适用于即时通讯、物联网等场景。其核心优势包括:

  • 高性能:基于 Java NIO 实现非阻塞通信
  • 易扩展:支持自定义协议和集群扩展
  • 简化配置:通过自动配置减少冗余代码

二、项目启动配置

1. 添加依赖

pom.xml 中引入 Tio-Boot 依赖(版本 3.6.0.v20200315-RELEASE):

<dependencies><dependency><groupId>org.tio</groupId><artifactId>tio-core-spring-boot-starter</artifactId><version>3.6.0.v20200315-RELEASE</version></dependency><dependency><groupId>org.tio</groupId><artifactId>tio-websocket-spring-boot-starter</artifactId><version>3.6.0.v20200315-RELEASE</version></dependency>
</dependencies>

2. 配置文件 application.yml

server:port: 8080tio:websocket:server:ip: 127.0.0.1heartbeat-timeout: 600000

3. 启动类配置

在 Spring Boot 主类添加 @EnableTioWebSocketServer 注解:

@SpringBootApplication
@EnableTioWebSocketServer
public class TioChatApplication {public static void main(String[] args) {SpringApplication.run(TioChatApplication.class, args);}
}
@Configuration
public class TioManualConfig {@Value("${tio.websocket.server.port:9326}")private int websocketPort;@Bean(initMethod = "start")public WsServerStarter wsServerStarter() throws IOException {return new WsServerStarter(websocketPort, new TioWebSocket());}
}

三、核心功能实现

1. 消息处理器(Handler)

创建 ChatServerAioHandler 处理消息收发:

@Component
@TioWebSocketMsgHandler
public class ChatServerAioHandler implements IWsMsgHandler {private static final String CHARSET = "UTF-8";@Overridepublic HttpResponse handshake(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) {String userId = httpRequest.getParam("uid");if (StrUtil.isBlank(userId)) {httpResponse.setStatus(HttpResponseStatus.C401);return httpResponse;}// 绑定用户到默认群组Tio.bindGroup(channelContext, "default_group");Tio.bindUser(channelContext, userId);return httpResponse;}@Overridepublic void onAfterHandshaked(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) {System.out.println("用户 " + channelContext.getToken() + " 已连接");}@Overridepublic Object onText(WsRequest wsRequest, String message, ChannelContext channelContext) {System.out.println("收到消息: " + message);JSONObject json = JSON.parseObject(message);String type = json.getString("type");String content = json.getString("content");String target = json.getString("target");if ("private".equals(type)) {// 私聊逻辑ChannelContext targetContext = Tio.getChannelContextByToken(channelContext.getGroupContext(), target);if (targetContext != null) {Tio.send(targetContext, content.getBytes(CHARSET));}} else if ("group".equals(type)) {// 群聊逻辑Tio.sendToGroup(channelContext, "default_group", content.getBytes(CHARSET));} else if ("group_custom".equals(type)) {// 组聊逻辑(自定义群组)String groupId = json.getString("groupId");Tio.sendToGroup(channelContext, groupId, content.getBytes(CHARSET));}return null;}@Overridepublic void onClose(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) {String userId = Tio.getUserContext(channelContext).getToken();System.out.println("用户 " + userId + " 已断开");Tio.unbindUser(channelContext, userId);Tio.unbindGroup(channelContext, "default_group");}
}

2. 用户与群组管理

@Component
public class UserService {private static final Map<String, ChannelContext> USER_MAP = new ConcurrentHashMap<>();public void bindUser(ChannelContext channelContext, String userId) {USER_MAP.put(userId, channelContext);}public void unbindUser(String userId) {ChannelContext context = USER_MAP.remove(userId);if (context != null) {Tio.close(context);}}public ChannelContext getUserContext(String userId) {return USER_MAP.get(userId);}
}

3. 组聊扩展(高级功能)

@Component
public class GroupService {private static final Map<String, Set<ChannelContext>> GROUP_MAP = new ConcurrentHashMap<>();public void joinGroup(ChannelContext context, String groupId) {GROUP_MAP.computeIfAbsent(groupId, k -> ConcurrentHashMap.newKeySet()).add(context);}public void leaveGroup(ChannelContext context, String groupId) {GROUP_MAP.getOrDefault(groupId, Collections.emptySet()).remove(context);}public void sendToGroup(String groupId, byte[] message) {GROUP_MAP.getOrDefault(groupId, Collections.emptySet()).forEach(ctx -> {try {ctx.writeAndFlush(new String(message, CHARSET));} catch (Exception e) {e.printStackTrace();}});}
}

四、测试与验证

1. 启动服务

运行 TioChatApplication,控制台输出:

TioWebSocketServer 启动成功,监听端口:8081

2. 客户端连接

使用 WebSocket 客户端连接 ws://localhost:8081,发送测试消息:

{"type": "private","target": "user123","content": "Hello Tio!"
}

3. 验证功能

  • 私聊:指定 typeprivatetarget 为目标用户 ID
  • 群聊:发送消息时指定 typegroup
  • 组聊:发送消息时指定 typegroup_custom 并携带 groupId 字段

Tio可以更好的拓展websocket,更方便的实现私聊,组聊和群聊的功能,其内部封装好了许多方法,详细可以查看官方文档

http://www.dtcms.com/wzjs/167697.html

相关文章:

  • 海淘哪些网站做攻略好怎么做推广和宣传
  • 如何做情趣网站seo关键词优化推广外包
  • 兰州网站优化seo研究中心晴天
  • 免费网站建设程序下载单页网站排名优化
  • wordpress魔板优化关键词的正确方法
  • 绍兴柯桥哪里有做网站的网络营销推广平台有哪些
  • 商梦建站百度浏览官网
  • 免费网站申请域名澳门seo知识分享
  • 沙洋网站定制站点查询
  • 国内网站设计案例欣赏常用的网络推广方式有哪些
  • 深圳网站设计制作公司 维仆网络营销策划书的范文
  • 广州市中智软件开发有限公司seo优化报价公司
  • 建设网站怎么做站长之家权重查询
  • 做网站需要相机吗刷网站seo排名软件
  • 电子商务网站建设学什么做一个公司网站要多少钱
  • 医疗器械网上商城优化大师怎么样
  • 如何做网站劫持广州最新重大新闻
  • 上海网站制作网站开发口碑推广
  • 自己做网站 搜索功能开发国际大新闻最新消息
  • 网站可以做软件检测吗佛山seo
  • 做网站客户改来改去手机百度高级搜索入口在哪里
  • 口碑好的网站建设哪家好关键词怎么提取
  • 美联社中文新闻福建搜索引擎优化
  • 建设网站技术人员先进事迹最新军事新闻 今日 最新消息
  • 山东济南网站建设十大外贸平台
  • 小型网站怎样优化中文搜索引擎排名
  • 最新网站制作郑州seo团队
  • 地方志网站建设网络推广的方法有哪些
  • 网上营销渠道的功能网站seo重庆
  • 网站开发有哪些风险中国关键词官网