引言

T-io网络通讯框架开源之后受到许多同学的喜欢,但是对于使用Spring系列技术的同学用起来稍许不适。于是乎抽时间写了个 starter,很荣幸代码被作者采纳,正式入驻T-io家族包。

tio-websocket-server

tio-websocket-server 是基于tio的websocket协议的实现。并且,无缝对接点对点发送消息,群组消息,客户端连接维护等。所以,基于 tio-websocket-server 的 starter,在使用起来不必关心连接的维护问题,只要做好业务处理即可。在拥有了tio的便利性的同时又可以使用spring的各种特性,极大的简化了spring + websocket 的开发难度。

快速开始

引入 jar 包

org.t-io

tio-websocket-spring-boot-starter

3.3.2.v20190601-RELEASE

添加注解

@SpringBootApplication

@EnableTioWebSocketServer

public class SamplesApplication {

public static void main(String[] args) {

SpringApplication.run(SamplesApplication.class,args);

}

}

修改配置文件

tio:

websocket:

server:

port: 9876

heartbeat-timeout: 60000

#是否支持集群,集群开启需要redis

cluster:

enabled: false

redis:

ip: 192.168.1.225

port: 6379

编写消息处理类

public class MyWebSocketMsgHandler implements IWsMsgHandler {

@Override

public HttpResponse handshake(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) throws Exception {

return httpResponse;

}

@Override

public void onAfterHandshaked(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) throws Exception {

System.out.println("握手成功");

}

@Override

public Object onBytes(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception {

System.out.println("接收到bytes消息");

return null;

}

@Override

public Object onClose(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception {

return null;

}

@Override

public Object onText(WsRequest wsRequest, String s, ChannelContext channelContext) throws Exception {

System.out.println("接收到文本消息:"+s);

return null;

}

}

编写简单客户端

var ws =new WebSocket("ws://localhost:9876");

ws.onopen = function (event) {

console.log("opened");

ws.send("Hello Tio WebSocket");

}

ws.onmessage=function (p1) {

console.log(p1.data);

}

运行程序,打开浏览器,Console 打印

opened

服务器收到了消息:Hello Tio WebSocket

原生回调接口支持

通过包扫描,反射创建新实例,赋给 TioWebSocketServerBootstrap中的属性。客户端编写相应的类即可,如下:

//最主要的逻辑处理类,必须要写,否则 抛异常

public class MyWebSocketMsgHandler implements IWsMsgHandler {}

//可不写

public class SpringBootAioListener extends WsServerAioListener {}

//可不写

public class SpringBootGroupListener implements GroupListener {}

//可不写

public class SpringBootIpStatListener implements IpStatListener {}

服务端主动推送

主动推送消息,核心在于获取 ServerGroupContext。

@RestController

@RequestMapping("/push")

public class PushController {

@Autowired

private TioWebSocketServerBootstrap bootstrap;

@GetMapping("/msg")

public void pushMessage(String msg){

if (StrUtil.isEmpty(msg)){

msg = "hello tio websocket spring boot starter";

}

Tio.sendToAll(bootstrap.getServerGroupContext(), WsResponse.fromText(msg,"utf-8"));

}

}

SSL 支持

配置文件中增加如下配置

# SSL 配置

ssl:

enabled: true

key-store: key-store path

password: password

trust-store: trust-store path

集群支持

# 集群配置 默认关闭

cluster:

enabled: false

# 集群是通过redis的Pub/Sub实现,所以需要配置Redis

redis:

ip: 127.0.0.1

port: 6379

all: true

group: true

ip: true

user: true

总结

tio-websocket-spring-boot-starter的宗旨和所有spring boot starter一致,通过配置实现快速开发。而又不会失去tio的代码灵活性。通过配置文件可以自由配置相应的功能,例如 SSL,集群等。注:本项目并未改变使用tio开发业务代码的方式。例如:Tio类的各种功能,同样适用。

注意

由于 bean 的加载顺序不同,可能在不同的功能类中注入tio-websocket-starter中的bean会有意想不到的问题。但是,基本很少,最常用的基本就是上文中的TioWebSocketServerBootstrap了,用它来获取ServerGroupContext,毕竟ServerGroupContext才是核心。

其他功能

具体参考 tio-websocket-server 项目。

源码地址

示例地址

官方文档地址

Tio消息服务器,tio-websocket-spring-boot-starter 的简单使用相关推荐

  1. 一个项目有两个pom_实现一个Spring Boot Starter超简单,读 Starter 源码也不在话下...

    Spring Boot 对比 Spring MVC 最大的优点就是使用简单,约定大于配置.不会像之前用 Spring MVC 的时候,时不时被 xml 配置文件搞的晕头转向,冷不防还因为 xml 配置 ...

  2. 实现一个 Spring Boot Starter 原来如此简单,读 Starter 源码也不在话下

    我是风筝,公众号「古时的风筝」,一个在程序圈混迹多年,主业 Java,另外 Python.React 也玩儿的 6 的斜杠开发者.现已转行程序员鼓励师 Spring Cloud 系列文章已经完成,可以 ...

  3. Tio消息服务器,tio-websocket-spring-boot-starter 的简单使用 原 荐

    引言 T-io 网络通讯框架开源之后受到许多同学的喜欢,但是对于使用Spring系列技术的同学用起来稍许不适.于是乎抽时间写了个 starter,很荣幸代码被作者采纳,正式入驻 T-io 家族包. t ...

  4. Spring Boot Starter 常用列表

    文章目录 1.美图 2.常用列表 3.源码中的starters 1.美图 2.常用列表 以下为常⽤用的 Spring Boot Starter 列列表. 名称 描述 Pom spring-boot-s ...

  5. RabbitMQ(九):RabbitMQ 延迟队列,消息延迟推送(Spring boot 版)

    应用场景 目前常见的应用软件都有消息的延迟推送的影子,应用也极为广泛,例如: 淘宝七天自动确认收货.在我们签收商品后,物流系统会在七天后延时发送一个消息给支付系统,通知支付系统将款打给商家,这个过程持 ...

  6. 手把手教你定制标准 Spring Boot starter

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 写在前面 我们每次构建一个 Spring 应用程序时,我 ...

  7. 一个简易上手的短信服务Spring Boot Starter,连傻瓜都会!

    作 者:jackieonway 来 源:jianshu.com/u/36510c75d37c 短信服务在用户注册.登录.找回密码等相关操作中,可以让用户使用更加便捷,越来越多的公司都采用短信验证的方式 ...

  8. 快速开发一个自定义 Spring Boot Starter ,希望你也会

    来源:http://t.cn/Ai9li9fC 众所周知,Spring Boot由众多Starter组成,随着版本的推移Starter家族成员也与日俱增.在传统Maven项目中通常将一些层.组件拆分为 ...

  9. 自定义 Spring Boot Starter

    一.引言 什么是Spring Boot Starter呢?我们直接来看看官网是怎么介绍的吧. Starters are a set of convenient dependency descripto ...

最新文章

  1. 解决在VS(winform)程序中无法在调试时修改代码的问题(也就是“编辑并继续”功能无效)...
  2. 在linux上安装Filezilla文件传输FTP软件
  3. 基于FPGA的波速形成实现
  4. Dubbo 2.7三大特性详解
  5. Linux设置SFTP服务用户目录权限
  6. [渝粤教育] 西南石油大学 数据库原理及应用 参考 资料
  7. 更换mysql_用这47 张图带你 MySQL 进阶!!!
  8. Java删除ArrayList中的重复元素的2种方法
  9. 知道创宇获CNNVD年度优秀技术支撑单位及漏洞预警报送专项奖
  10. java list 转json 字符串_fastjson将java list转为json字符串
  11. c语言标准库详解(一):stdio.h之文件操作
  12. 谷歌浏览器扩展权限的问题:允许扩展程序读取和更改网站数据
  13. mrpoid模拟器java版_mrpoid2冒泡模拟器下载-mrpoid2模拟器下载3.0安卓版-西西软件下载...
  14. 00023 自考高等数学
  15. blender2.8设置玻璃材质
  16. iPad远程控制windows主机及内网穿透原理
  17. 麒麟信安参与共建的 中国首个桌面操作系统根社区openKylin即将发布
  18. ASEMI三端正线性稳压器78M05,78M05参数,78M05特征
  19. Nature综述:肠道菌群如何划分肠型
  20. 依赖计算机英语作文,我们是否太依赖电脑英语作文

热门文章

  1. kubeadm1.22.x 部署kubernetes集群
  2. element-UI日期时间选择器el-date-picker
  3. 哈理工 校赛(热身赛)2238 围巾的纠结(判断回路问题)
  4. 启动Tomcat报Artifact is being deployed, please wait问题
  5. [ LeetCode ] #17. Letter Combinations of a Phone Number(电话按键组合字符 C++ Python)
  6. python多路分支_用于多个参数的python multiprocessing pool.map
  7. ctf crypto writeup
  8. 黑帽技术以及百度惩罚网站的原因和方式以及解决方式
  9. 年报背后:顺丰疯狂的错位价格战
  10. Laputa在cnblogs