tio-websocket-spring-boot-starter 的简单使用
引言
T-io
网络通讯框架开源之后受到许多同学的喜欢,但是对于使用Spring系列技术的同学用起来稍许不适。于是乎抽时间写了个 starter,很荣幸代码被作者采纳,正式入驻T-io
家族包。
tio-websocket-server
tio-websocket-server 是基于tio的websocket协议的实现。并且,无缝对接点对点发送消息,群组消息,客户端连接维护等。所以,基于 tio-websocket-server 的 starter,在使用起来不必关心连接的维护问题,只要做好业务处理即可。在拥有了tio的便利性的同时又可以使用spring的各种特性,极大的简化了spring + websocket 的开发难度。
快速开始
- 引入 jar 包
<dependency>
<groupId>org.t-io</groupId>
<artifactId>tio-websocket-spring-boot-starter</artifactId>
<!--此版本号跟着tio主版本号一致即可-->
<version>3.3.2.v20190601-RELEASE</version>
</dependency>
- 添加注解
@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;
}
}
- 编写简单客户端
<script>
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);
}
</script>
- 运行程序,打开浏览器,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 项目。
源码地址
示例地址
官方文档地址

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
Dubbo服务暴露与注册
前面的文章中,我们讲解了Dubbo是如何进行配置的属性的初始化的,并且讲到,Dubbo最终会将所有的属性参数都封装为一个URL对象,从而以这个URL对象为基准传递参数。本文则主要讲解Dubbo是如何基于URL对象进行服务的暴露与注册的。 首先需要说明的一点是,服务的暴露与注册是两个不同的概念。在Dubbo中,微服务之间的交互默认是通过Netty进行的,而服务之间的通信是基于TCP以全双工的方式进行的。那么也就是说,每个服务都会存在一个ip和port。所谓的服务暴露就是指根据配置将当前服务使用Netty绑定一个本地的端口号(对于消费者而言,则是尝试连接目标服务的ip和端口)。至于注册,由于微服务架构中对于新添加的服务,需要一定的机制来通知消费者,有新的服务可用,或者对于某些下线的服务,也需要通知消费者,将这个已经下线的服务给移除。Dubbo中服务的注册与发现默认是委托给zookeeper来进行的。本文主要讲解服务的暴露与注册的整体实现结构,至于服务暴露和注册时所需要注意的详细细节,则在后面的文章中进行讲解。 1. 服务的暴露 服务的暴露的入口位置主要在RegistryProtocol.e...
-
下一篇
act-morphia 1.7.2 带来不一样的数据聚合体验
1. 概述 Mongodb 2.2 开始就提供了数据Aggregation Pipeline (聚合管道)用于简单数据分析统计,包括计数(count),求和(sum),均值(average),标准差(stddev) 等. 这个特性相较以前的 Map Reduce 方式提升了很多. 遗憾的是在服务端代码上使用 Aggregation Pipeline 还是需要使用比较繁复的 API, 包括 Spring Data 和 Morphia 提供的 API. 这大多是因为 Aggregation Pipeline 需要兼顾各种情况, 比如嵌入数组的 rewind, 还有对第一次聚合数据进行再聚合等. 在很多常用情况下, 应用只需要简单的分组聚合, 最多对聚合结果数据进行过滤和排序. 这时候我们希望能通过更简单的方式来获得结果. Act-morphia 插件在随 act-starter-1.8.23.2 最新发布的 1.7.2 版本中提供了一组简单易用的 API 来实现常用聚合逻辑. 2. API 简介 Act-Morphia 依托与 Morphia 库 实现了 Act-DB 框架. 下面是 Ac...
相关文章
文章评论
共有0条评论来说两句吧...