鄭州網(wǎng)絡(luò)優(yōu)化實(shí)力樂云seo百度seo快速排名優(yōu)化軟件
前言
我最近一個(gè)月一直在尋找能夠快速開發(fā)實(shí)時(shí)通訊的簡單好用的模塊,所以我就去尋找了一下相關(guān)的內(nèi)容.在此之前我使用的是Spring原生的webSocket,她有個(gè)弊端就是設(shè)置組不容易設(shè)置,而且配置上也稍微復(fù)雜一點(diǎn),需要配置攔截器和處理器,還需要把它放入到Springboot的啟動(dòng)容器里面,也有個(gè)好處就是,服務(wù)端口是單服務(wù)的,而且定制性強(qiáng),就是很多東西都需要自己來主動(dòng)配置,讓我頭疼的就是
組
的配置
在此之后我就去找尋能讓我寫的代碼最少,而且還易用的服務(wù),就發(fā)現(xiàn)了TIO
,下面先看一下簡介
TIO
極致打磨的底層集群能力,可無縫解決IM、物聯(lián)網(wǎng)等大型產(chǎn)品的集群需求
易學(xué)易用,讓剛畢業(yè)的大學(xué)生也能輕易駕馭
全方位開箱即用的監(jiān)控能力
實(shí)戰(zhàn)中仍表現(xiàn)出卓越的性能,不用實(shí)驗(yàn)室數(shù)據(jù)忽悠智慧的大眾
內(nèi)置協(xié)議適配能力,讓多協(xié)議接入不再難
內(nèi)置ack消息能力,讓RPC等場景輕松實(shí)現(xiàn)
自創(chuàng)同步鎖、同步安全線程池、同步數(shù)據(jù)結(jié)構(gòu)等工具庫,為業(yè)務(wù)應(yīng)用提供豐富的開箱即用API
內(nèi)置半包粘包處理
豐富的生態(tài),目前已經(jīng)用t-io實(shí)現(xiàn)了http、websocket、mqtt及大量私有協(xié)議
內(nèi)置慢攻擊防御機(jī)制,幫助應(yīng)用自動(dòng)拉黑嫌疑IP看了上面的話,大概意思就是,簡單易學(xué),而且是國人開發(fā)的,內(nèi)部有自動(dòng)處理半包和粘包處理機(jī)制,無需再重新處理粘包,接下來就是Tio的具體配置代碼
正文
但是今天我們要用的并不是
tio-websocket-server
而是另外一個(gè)更加簡單的服務(wù),不過這個(gè)已經(jīng)在2020年停止更新了,好像是最后一版java1.8版本的,我看了Tio最新版都是用java17的了,如果還在用java1.8的兄弟們可以使用這個(gè)依賴tio-websocket-spring-boot-starter
,下面是依賴的完整展示
1.POM導(dǎo)入依賴
<!--t-io依賴--><dependency><groupId>org.t-io</groupId><artifactId>tio-websocket-spring-boot-starter</artifactId><version>3.6.0.v20200315-RELEASE</version></dependency>
2.YML配置文件中放入以下配置
# Tio 配置信息 tio:websocket:server:port: 9876#心跳超時(shí)時(shí)間heartbeat-timeout: 60000#是否支持集群,集群開啟需要rediscluster:enabled: falseredis:ip: localhostport: 6379
3.啟動(dòng)類加入注解
注解:
@EnableTioWebSocketServer
4.創(chuàng)建處理器
package com.xssq.handler;import com.xssq.service.ApiService; import org.springframework.beans.factory.annotation.Autowired; 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.server.handler.IWsMsgHandler;/** * tio網(wǎng)絡(luò)套接字味精處理程序 * * @author xssq * @version 1.0.0 * @date 2023/09/26 * @wisdom 你可以不會,但你不能不知道 */ @Component public class TioWebSocketMsgHandler implements IWsMsgHandler {@Autowiredprivate ApiService apiService;/*** 握手** @param httpRequest http請求* @param httpResponse http響應(yīng)* @param channelContext 渠道上下文* @return {@link HttpResponse}*/@Overridepublic HttpResponse handshake(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) {String token = httpRequest.getParam("token");channelContext.setToken(token);Tio.bindGroup(channelContext, "1");Tio.bindUser(channelContext, "1");System.out.println("handshake握手方法");return httpResponse;}/*** 握手后打開** @param httpRequest http請求* @param httpResponse http響應(yīng)* @param channelContext 渠道上下文*/@Overridepublic void onAfterHandshaked(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) {System.out.println("onAfterHandshaked握手后打開的");}/*** 在字節(jié)上** @param wsRequest ws請求* @param bytes 字節(jié)* @param channelContext 渠道上下文* @return {@link Object}*/@Overridepublic Object onBytes(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) {System.out.println("onBytes方法");return null;}/*** 關(guān)閉** @param wsRequest ws請求* @param bytes 字節(jié)* @param channelContext 渠道上下文* @return {@link Object}*/@Overridepublic Object onClose(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) {System.out.println("onClose關(guān)閉方法");return null;}/*** 在文本上** @param wsRequest ws請求* @param channelContext 渠道上下文* @param message 信息* @return {@link Object}*/@Overridepublic Object onText(WsRequest wsRequest, String message, ChannelContext channelContext) {if ("心跳包".equals(message)) {return null;}apiService.sendMsg(channelContext, message);return null;} }
5.創(chuàng)建監(jiān)聽器
package com.xssq.listener;import org.springframework.stereotype.Component; import org.tio.core.ChannelContext; import org.tio.core.intf.GroupListener;/** * spring boot組偵聽器 * * @author xssq * @version 1.0.0 * @date 2023/10/05 * @wisdom 你可以不會,但你不能不知道 */ @Component public class SpringBootGroupListener implements GroupListener {/*** 綁定后打開** @param channelContext 信道上下文* @param s s*/@Overridepublic void onAfterBind(ChannelContext channelContext, String s) {System.out.println("綁定后打開");}/*** 在解除綁定后打開** @param channelContext 信道上下文* @param s s*/@Overridepublic void onAfterUnbind(ChannelContext channelContext, String s) {System.out.println("在解除綁定后打開");} }
6.到此整個(gè)教程也就結(jié)束了,其實(shí)那個(gè)監(jiān)聽器也可以不要,不過用于方便綁定組下面是一些綁定組的語法
/*綁定組*/ Tio.bindGroup(channelContext, "1"); /*綁定userId*/ Tio.bindUser(channelContext, "1"); ...... ...
其實(shí)還有很多綁定的,還有綁定業(yè)務(wù)方法,綁定token的,很多,下面是一些服務(wù)說明
//最主要的邏輯處理類,必須要寫,否則 拋異常 public class MyWebSocketMsgHandler implements IWsMsgHandler {} //可不寫 public class SpringBootAioListener extends WsServerAioListener {} //可不寫 public class SpringBootGroupListener implements GroupListener {} //可不寫 public class SpringBootIpStatListener implements IpStatListener {}
后記
操作完上面的內(nèi)容就可以正常使用了,感謝大家觀看
如果我的博客幫助到了您,您可以到我的博客
https://blog.csdn.net/weixin_57228276
或者微信公眾號搜索幸識SQ
,在那里可以找到我,里面也有更多的優(yōu)秀文章