一、前言
本文講述 基于springboot + netty 實(shí)現(xiàn)websocket服務(wù)端搭建。
使用springboot 集成 netty-websocket-spring-boot-starter 來實(shí)現(xiàn)websocket
也有直接通過netty 代碼方式實(shí)現(xiàn)的方式但是不推薦速址,傳統(tǒng)方式暫時我還無法找到通過url的方式來進(jìn)行路由區(qū)分的潘拱,需要每個業(yè)務(wù)場景定義一個端口东臀。
netty代碼方式實(shí)現(xiàn)地址:https://blog.csdn.net/qq825478739/article/details/126263050
需要了解netty的 請點(diǎn)擊網(wǎng)址查看我總結(jié)的一些netty的介紹
netty介紹地址:還沒完善后邊不上
netty-websocket-spring-boot-starter gitee地址:https://gitee.com/Yeauty/netty-websocket-spring-boot-starter
二坯苹、快速開始
1. 引入pom坐標(biāo)
<dependency>
<groupId>org.yeauty</groupId>
<artifactId>netty-websocket-spring-boot-starter</artifactId>
<version>0.9.5</version>
</dependency>
2. websocket實(shí)現(xiàn)
/**
* 當(dāng)springboot 項(xiàng)目啟動會通過自動裝配 找到netty-websocket-spring-boot-starter 中META-INF 加載 NettyWebSocketAutoConfigure
* 通過@EnableWebSocket -> @Import({NettyWebSocketSelector.class})
* 在 NettyWebSocketSelector 中會創(chuàng)建 Bean ServerEndpointExporter 被Spring聲明并使用
*
* ServerEndpointExporter 類通過Spring配置聲明并被使用,
* ServerEndpointExporter 將會去掃描帶有@ServerEndpoint 注解的類注冊成為一個WebSocket斷電叶沛。
* 所有的配置項(xiàng)都在這個@ServerEndpoint注解屬性中 ( 如:@ServerEndpoint("/ws") )
*
*/
//TODO 不需要加Spring的注解加載, 多例的,每次請求過來都會創(chuàng)建一個新的可以通過蒲讯。無參構(gòu)造查看
// port 默認(rèn)80 host默認(rèn) 0.0.0.0
@ServerEndpoint(path = "/ws/{arg}",port = "80",host = "0.0.0.0")
public class MyWebSocket {
public MyWebSocket() {
System.out.println("通過這里可以看到 每次請求過來都會創(chuàng)建");
}
/**
* 當(dāng)有新的連接進(jìn)入時,對該方法進(jìn)行回調(diào) 注入?yún)?shù)的類型 一般用不是 可以去掉
*
* @param session
* @param headers
* @param req
* @param reqMap
* @param arg
* @param pathMap
*/
@BeforeHandshake
public void handshake(Session session, HttpHeaders headers, @RequestParam String req, @RequestParam MultiValueMap reqMap, @PathVariable String arg, @PathVariable Map pathMap){
session.setSubprotocols("stomp");
if (!"ok".equals(req)){
System.out.println("Authentication failed!");
session.close();
}
}
/**
* 當(dāng)有新的WebSocket連接完成時灰署,會調(diào)用這個方法
*
* @param session
* @param headers
* @param req
* @param reqMap
* @param arg
* @param pathMap
*/
@OnOpen
public void onOpen(Session session, HttpHeaders headers, @RequestParam String req, @RequestParam MultiValueMap reqMap, @PathVariable String arg, @PathVariable Map pathMap){
System.out.println("new connection");
System.out.println(req);
}
/**
* 當(dāng)有WebSocket 關(guān)閉連接時 調(diào)用
*
* @param session
* @throws IOException
*/
@OnClose
public void onClose(Session session) throws IOException {
System.out.println("one connection closed");
}
/**
* 當(dāng)有WebSocket 拋出異常 調(diào)用
*
* @param session
* @param throwable
*/
@OnError
public void onError(Session session, Throwable throwable) {
throwable.printStackTrace();
}
/**
* 當(dāng)接收到字符串消息時判帮,調(diào)用
* message 請求參數(shù) 只能用字符串接收否則會報(bào)錯 類型轉(zhuǎn)換異常
*
* @param session
* @param message
*/
@OnMessage
public void onMessage(Session session, String message) {
System.out.println(message);
//TODO 發(fā)送消息
session.sendText("Hello Netty!");
}
/**
* 當(dāng)接收到二進(jìn)制消息時 調(diào)用
*
* @param session
* @param bytes
*/
@OnBinary
public void onBinary(Session session, byte[] bytes) {
for (byte b : bytes) {
System.out.println(b);
}
session.sendBinary(bytes);
}
/**
* 當(dāng)接收到Netty的事件時 調(diào)用
*
* @param session
* @param evt
*/
@OnEvent
public void onEvent(Session session, Object evt) {
if (evt instanceof IdleStateEvent) {
IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
switch (idleStateEvent.state()) {
case READER_IDLE:
System.out.println("read idle");
break;
case WRITER_IDLE:
System.out.println("write idle");
break;
case ALL_IDLE:
System.out.println("all idle");
break;
default:
break;
}
}
}
}
3. 啟動成功
image.png
三、總結(jié)
在使用過程中遇到一個問題 ,當(dāng) onMessage 方法 接收參數(shù)用對象接收的話會報(bào)錯溉箕。
我以為@RequestParam
錯誤使用方式
image.png
image.png
將對象改成字符串接收晦墙。再用Json轉(zhuǎn)換把。