本篇文章带领大家如何在 SpringBoot 中整合 tio-websocket-server 搭建一个自己的 websocket 服务器

1、引入 maven 依赖

<!-- tio-websocket -->
<dependency><groupId>org.t-io</groupId><artifactId>tio-websocket-server</artifactId><version>3.5.9.v20200214-RELEASE</version>
</dependency>

注意:我这里用的是 3.5.9.v20200214-RELEASE 版本,各个版本之间存在差异,请查看官方文档

2、websocket 配置类

import com.asurplus.tio.websocket.handle.MyWsMsgHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.tio.server.ServerTioConfig;
import org.tio.websocket.server.WsServerStarter;import java.io.IOException;/*** websocket 配置类*/
@Configuration
public class WebSocketConfig {/*** 注入消息处理器*/@Autowiredprivate MyWsMsgHandler myWsMsgHandler;/*** 启动类配置** @return* @throws IOException*/@Beanpublic WsServerStarter wsServerStarter() throws IOException {// 设置处理器WsServerStarter wsServerStarter = new WsServerStarter(6789, myWsMsgHandler);// 获取到ServerTioConfigServerTioConfig serverTioConfig = wsServerStarter.getServerTioConfig();// 设置心跳超时时间,默认:1000 * 120serverTioConfig.setHeartbeatTimeout(1000 * 120);// 启动wsServerStarter.start();return wsServerStarter;}
}

这里我们注入了 WsServerStarter 的 bean,这样在 SpringBoot 启动的时候,就能启动咱们的 websocket 服务

  • 注明了 websocket 的服务端口为:6789
  • 消息处理类为:myWsMsgHandler,在下一步我们将会去实现这个类
  • 设置了心跳的超时时间为:120秒,默认值,可以不设置

3、消息处理类

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.common.WsResponse;
import org.tio.websocket.server.handler.IWsMsgHandler;/*** 消息处理类*/
@Component
public class MyWsMsgHandler implements IWsMsgHandler {/*** <li>对httpResponse参数进行补充并返回,如果返回null表示不想和对方建立连接,框架会断开连接,如果返回非null,框架会把这个对象发送给对方</li>* <li>注:请不要在这个方法中向对方发送任何消息,因为这个时候握手还没完成,发消息会导致协议交互失败。</li>* <li>对于大部分业务,该方法只需要一行代码:return httpResponse;</li>** @param httpRequest* @param httpResponse* @param channelContext* @return* @throws Exception* @author tanyaowu*/@Overridepublic HttpResponse handshake(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) throws Exception {// 可以在此做一些业务逻辑,返回null表示不想连接return httpResponse;}/*** 握手成功后触发该方法** @param httpRequest* @param httpResponse* @param channelContext* @throws Exception* @author tanyaowu*/@Overridepublic void onAfterHandshaked(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) throws Exception {// 拿到用户idString id = httpRequest.getParam("id");// 绑定用户Tio.bindUser(channelContext, id);// 给用户发送消息WsResponse wsResponse = WsResponse.fromText("如果您看到此消息,表示您已成功连接 WebSocket 服务器", "UTF-8");Tio.sendToUser(channelContext.tioConfig, id, wsResponse);}/*** <li>当收到Opcode.BINARY消息时,执行该方法。也就是说如何你的ws是基于BINARY传输的,就会走到这个方法</li>** @param wsRequest* @param bytes* @param channelContext* @return 可以是WsResponse、byte[]、ByteBuffer、String或null,如果是null,框架不会回消息* @throws Exception* @author tanyaowu*/@Overridepublic Object onBytes(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception {return null;}/*** 当收到Opcode.CLOSE时,执行该方法,业务层在该方法中一般不需要写什么逻辑,空着就好** @param wsRequest* @param bytes* @param channelContext* @return 可以是WsResponse、byte[]、ByteBuffer、String或null,如果是null,框架不会回消息* @throws Exception* @author tanyaowu*/@Overridepublic Object onClose(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception {// 关闭连接Tio.remove(channelContext, "WebSocket Close");return null;}/*** <li>当收到Opcode.TEXT消息时,执行该方法。也就是说如何你的ws是基于TEXT传输的,就会走到这个方法</li>** @param wsRequest* @param text* @param channelContext* @return 可以是WsResponse、byte[]、ByteBuffer、String或null,如果是null,框架不会回消息* @throws Exception* @author tanyaowu*/@Overridepublic Object onText(WsRequest wsRequest, String text, ChannelContext channelContext) throws Exception {WsResponse wsResponse = WsResponse.fromText("服务器已收到消息:" + text, "UTF-8");Tio.sendToUser(channelContext.tioConfig, channelContext.userid, wsResponse);return null;}
}

我们实现了 IWsMsgHandler 接口,并重写了该接口的 5 个方法,这 5 个方法从 发送握手包,到消息收发,到断开连接等一系列过程

4、启动服务


启动成功后,可以看出 tio 的打印结果,我们可以看出服务端口为我们设置的 6789,我们便可以连接测试了

5、连接测试

  • 在线测试地址
http://coolaf.com/tool/chattest
  • 连接地址:
ws://127.0.0.1:6789/?id=1
  • 连接成功

    可以看出我们已经成功连接上了 websocket 服务器

  • 发送消息


成功发送消息

如您在阅读中发现不足,欢迎留言!!!

【tio-websocket】2、SpringBoot整合tio-websocket-server相关推荐

  1. Springboot整合Websocket遇到的坑_websocket session不支持序列化,无法存储至redis_Websocket相关问题总结(Session共享,用户多端登录等)

    Springboot整合Websocket遇到的坑 一.使用Springboot内嵌的tomcat启动websocket 1.添加ServerEndpointExporter配置bean @Confi ...

  2. Springboot 整合Websocket+Stomp协议+RabbitMQ做消息代理 实例教程

    前言 如果你还没有了解过websocket,关于整合websocket的简单入门使用,可以先看看我这篇: <SpringBoot 整合WebSocket 简单实战案例> https://b ...

  3. SpringBoot 整合WebSocket 简单实战案例

    前言 这个简单实战案例主要目的是让大家了解websocket的一些简单使用. 另外使用stomp方式的: <Springboot 整合 WebSocket ,使用STOMP协议 ,前后端整合实战 ...

  4. SpringBoot整合websocket实现在线客服聊天

    websocket最伟大之处在于服务器和客户端可以在给定的时间范围内的任意时刻,相互推送信息. 浏览器和服务器只需要要做一个握手的动作,在建立连接之后,服务器可以主动传送数据给客户端,客户端也可以随时 ...

  5. Springboot整合WebSocket实现网页版聊天,快来围观!

    前几天写了一篇<SpringBoot快速入门>一文,然后周末趁着有时间,在这个Springboot框架基础上整合了WebSocket技术写了一个网页版聊天功能. 如果小伙伴找不到那套框架了 ...

  6. Springboot 整合 WebSocket ,使用STOMP协议+Redis 解决负载场景问题(二)

    前言 上一篇,简单给大家整合了一下websocket,使用stomp方式. 这篇,就是考虑到单体的服务使用websocket ,按照上一篇的整合,确实没问题. 但是如果一旦是负载多台服务的时候,那么就 ...

  7. Springboot整合WebSocket(基于Stomp)

    Springboot整合WebSocket(基于Stomp) 文章目录 Springboot整合WebSocket(基于Stomp) 参考链接 前言 STOMP 定义 STOMP Over WebSo ...

  8. springboot整合websocket实现简易版单人聊天

    websockt在作为即时通讯类的聊天方面有较多的应用,其主要的特点就是轻量,使用方便,容易快速上手,通过webscoket整合服务端,就可以实现简单的类似聊天的功能,下面说说springboot整合 ...

  9. Websocket教程SpringBoot+Maven整合(详情)

    1.大话websocket及课程介绍 简介: websocket介绍.使用场景分享.学习课程需要什么基础 websocket介绍: WebSocket协议是基于TCP的一种新的网络协议.它实现了浏览器 ...

  10. 最简单的springboot整合websocket方式

    简介 WebSocket是一种与HTTP不同的协议.两者都位于OSI模型的应用层,并且都依赖于传输层的TCP协议. 虽然它们不同,但是RFC 6455中规定:it is designed to wor ...

最新文章

  1. AICompiler动态shape编译框架
  2. Java8自定义条件让集合分组
  3. pl/sql command window 初步接触
  4. 关于框架的胡言乱语(上)
  5. CF1472(div3):总结
  6. 无服务器安全性:将其置于自动驾驶仪上
  7. 最近在SDK下使用WebBrowser遇到了个问题
  8. 连接远程Windows主机中的虚拟机
  9. 块格式化上下文(Block Formatting Context,BFC)
  10. 基础层区块链Harmony发布主网新版本v4.0.0
  11. asp.net生成缩略图、文字图片水印
  12. oracle显示一个月的所有天数
  13. 反作弊基本概念与机器学习的应用(1)
  14. 镇魂街武神躯怎么修改服务器,镇魂街武神躯怎么重置守护灵_守护灵重置方法_3DM手游...
  15. svn认证失败两种解决方案(个人原创)
  16. LINUX定时清理文件定时任务
  17. 淘宝网前台应用性能优化实践
  18. 重启mysql的命令 linux_linux重启mysql命令
  19. 第三章 打造高性能的视频弹幕系统
  20. AI|优必选称准备IPO 此前公司机器人曾登上春晚舞台

热门文章

  1. 2022-2028全球与中国变压器测试服务市场现状及未来发展趋势
  2. HOG 行人检测 学习体会(如何制作训练样本)
  3. 只需简单几步 - 开始玩耍微信小程序
  4. 3行代码完成时序建模,最新开源的时序算法发布!
  5. 未来教育计算机二级c语言jd,逻辑思维训练地址_未来教育中心
  6. TypeScript类的使用
  7. 魔兽世界怀旧服最新服务器开发时间,魔兽世界怀旧服:牢记这几点,可以显著提高怀旧服升级速度...
  8. 2022-2028全球全氟己基甲醚行业调研及趋势分析报告
  9. Exchange Server 2010部署(一)部署Windows Server 2008 R2域控制器
  10. 高跟鞋踩猫、踩狗视频下载