tio-websocket-spring-boot-starter 的简单使用 原 荐

栏目: Java · 发布时间: 4年前

tio-websocket-spring-boot-starter 的简单使用 原 荐

tio-websocket-spring-boot-starter 的简单使用

  丶Pz 发布于 今天 08:16

字数 919

阅读 33

收藏 1

WebSocket t-io Spring Spring Boot

2019独角兽企业重金招聘 Python 工程师标准>>> 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 项目。

源码地址

示例地址

官方文档地址

© 著作权归作者所有

上一篇: 新人入住请多多关照

下一篇: 学习T-io框架,从写一个Redis客户端开始

tio-websocket-spring-boot-starter 的简单使用 原 荐

丶Pz

粉丝 70

博文 11

码字总数 18387

作品 0

程序员

提问

相关文章 最新文章

宿命/tio-websocket-spring-boot-starter

tio-websocket-spring-boot-starter Intro spring-spring-boot-starter for tio-websocket Starter Repo Github Sample Gitee Sample T-io Repo Gitee Sample Sample Repo Github Sample Git......

宿命

2018/11/07

0

0

t-io 2.4.0 发布, 不仅仅是百万级 TCP 长连接框架

修改点: 1、AioHandler.decode(ByteBuffer buffer, ChannelContext channelContext)方法签名改成AioHandler.decode(ByteBuffer buffer, int limit, int position, int readableLength, Cha......

talent-tan

2018/05/07

3.4K

21

tio-websocket 0.0.4 发布,和 tio 底层 API 无缝对接

tio-websocket是基于tio实现的websocket服务器,使用方式极其简单 本次修改点 1、t-io版本升级到2.0.2.v20171129-RELEASE 2、tio-http版本升级到0.0.4-tio-http 3、简单优化一下demo程序 最新...

talent-tan

2017/11/29

3.2K

7

talent-tan/tio-websocket-showcase

tio-websocket-showcase 项目介绍 展示tio-websocket的用法,官方提供的唯一tio-websocket示范教程 包括wss和流量监控及处理等高级特性 还包括t-io作者写的一个用于连接websocket服务器的js小...

talent-tan

2018/05/06

0

0

t-io 导入相关依赖包之后,junit不能正常使用

@talent-tan 你好,想跟你请教个问题:我的项目再导入t-io的tio-http-common和tio-websocket-common 两个包之后,我的junit的@Test 不能正常运行 导入的maven版本对应的为: org.t-io tio-h...

捭阖纵_横_

2017/09/05

202

2

没有更多内容

加载失败,请刷新页面

加载更多
Spark内置图像数据源初探

概述 在Apache Spark 2.4中引入了一个新的内置数据源, 图像数据源.用户可以通过DataFrame API加载指定目录的中图像文件,生成一个DataFrame对象.通过该DataFrame对象,用户可以对图像数据进行简...

阿里云官方博客

21分钟前

4

0

tio-websocket-spring-boot-starter 的简单使用 原 荐
掌握Composer

这一次,真正掌握composer composer是现代 PHP 的基石 现代高级编程语言,依赖管理 工具 是必不可少的。Java有Maven,Python有pip,Nodejs有npm, 而在composer出现之前,PHP只有被广为诟病的Pea...

城市之雾

27分钟前

3

0

tio-websocket-spring-boot-starter 的简单使用 原 荐
Shell中的函数、数组、告警系统

20.16/20.17 shell 中的函数 20.18 shell中的数组 20.19 告警系统需求分析 20.20 告警系统主脚本 20.21 告警系统配置文件 20.22 告警系统监控项目 20.23/20.24/20.25 告警系统邮件引擎 20.26 ...

tobej

28分钟前

1

0

Win7系统安装hadoop

环境准备 安装JDK1.8,配置JAVA_HOME 下载hadoop_3.1.2,配置HADOOP_HOME 配置HDFS 修改hadoop-env.cmd 增加 set HADOOP_PREFIX=%HADOOP_HOME%set HADOOP_CONF_DIR=%HADOOP_PREFIX%\etc\ha......

铲平王

31分钟前

2

0

IT兄弟连 Java 语法教程 Java语言的其他特性

Java语言中除了非常重要的跨平台特性外,还有如下几个关键特性: ● 语法简单易学 Java语言的语法简单明了,容易掌握,而且是纯面向对象(OOP)的语言,Java语言的简单性主要体现在以下几个方...

码农 的一亩三分地

44分钟前

4

0

没有更多内容

加载失败,请刷新页面

加载更多

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Sexy Web Design

Sexy Web Design

Elliot Stocks / SitePoint / 2009-03-28 / $39.95

Description A guide to building usable, aesthetically pleasing interfaces for web sites and web applications by applying timeless principles of user-centered design. This book focuses on practical ......一起来看看 《Sexy Web Design》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具