springboot使用webSocket的兩種方式
-
方法一 使用
@ServerEndpoint
註解方式
-
新增
ServerEndpointExporter
到IOC
容器中 -
ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
} -
使用
@ServerEndpoint
package cn.qqhxj;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.websocket.OnClose;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* @author han xinjian
* @date 2018-11-20 17:29
**/
@Component
@Slf4j
@ServerEndpoint(value = "/socket")
public class SocketHandler {
private static CopyOnWriteArraySet<SocketHandler> webSocketSet
= new CopyOnWriteArraySet<SocketHandler>();
private Session session;
@OnClose
public void onClose() {
webSocketSet.remove(this); //從set中刪除
}
@OnOpen
public void onOpen(Session session) {
this.session = session;
webSocketSet.add(this); //加入set中
log.info("歡迎id={}你的加入成功" , session.getId());
}
public void sendMessage(String string) throws IOException {
this.session.getBasicRemote().sendText(JSONObject.toJSONString(string));
//this.session.getAsyncRemote().sendText(message);
}
public static <T> void sendMessageToAll(String str) throws IOException {
for (SocketHandler item : webSocketSet) {
try {
item.sendMessage(str);
} catch (IOException e) {
continue;
}
}
}
}
-
方法二 繼承
TextWebSocketHandler
-
配置
@Configuration
-
class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {registry.addEndpoint("/api/v1/socket") //新增允許跨域訪問 .setAllowedOrigins("*") .withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {//推送訊息字首 registry.enableSimpleBroker("/api/v1/socket/send"); //應用請求字首 registry.setApplicationDestinationPrefixes("/api/v1/socket/req"); //推送使用者字首 registry.setUserDestinationPrefix("/user");
}
}
2. ### 繼承`TextWebSocketHandler`
package cn.qqhxj.wsn.modules.wsn.socket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* @author han xinjian
* @date 2018-12-26 13:06
**/
@Slf4j
@Component
public class BaseWebSocketHandler extends TextWebSocketHandler {
private static CopyOnWriteArraySet<WebSocketSession> socketSessions = new CopyOnWriteArraySet<>();
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
socketSessions.add(session);
}
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
socketSessions.remove(session);
}
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
System.out.println(message.getPayload());
}
}
相關文章
- SpringBoot 使用ApplicationContext 及 getbean的兩種方式Spring BootAPPContextBean
- 在springboot中使用Mybatis Generator的兩種方式Spring BootMyBatis
- ChatTTS的兩種使用方式TTS
- SpringBoot實現熱部署兩種方式!Spring Boot熱部署
- Redis詳解 - SpringBoot整合Redis,RedisTemplate和註解兩種方式的使用RedisSpring Boot
- SpringBoot使用WebSocketSpring BootWeb
- MyBatis 與 SpringBoot 整合:註解和xml兩種使用方式介紹MyBatisSpring BootXML
- ubuntu建立使用者的兩種方式Ubuntu
- Springboot中使用執行緒池的三種方式Spring Boot執行緒
- 一文搞懂四種 WebSocket 使用方式,建議收藏!!!Web
- Springboot啟動了哪些bean?這兩種方式可以獲取Spring BootBean
- JS 垃圾回收的兩種方式JS
- CommonJS的兩種匯出方式JS
- 建立Session物件的兩種方式Session物件
- Docker打包映象的兩種方式Docker
- Laravel 的觀察者使用記錄與兩種方式Laravel
- AUTOCAD——兩種延伸方式
- 多執行緒的建立 兩種方式以及使用建議執行緒
- zabbix agent 的兩種安裝方式
- Native 工程整合Flutter 的兩種方式Flutter
- 安裝aab包的兩種方式
- HTTP代理的兩種連線方式HTTP
- 隱藏元素兩種方式
- 基於Maven建立SpringBoot的2種方式MavenSpring Boot
- vue3使用ElementPlus upload上傳檔案的兩種方式Vue
- 《SpringBoot篇:002》《SpringBoot的三種啟動方式:main、Maven、jar》Spring BootAIMavenJAR
- php 與 nginx 的兩種處理方式PHPNginx
- React元件方法的兩種定義方式React元件
- java 傳遞引數的兩種方式Java
- java json格式化的兩種方式JavaJSON
- Java鍵盤錄入的兩種方式Java
- Python進行開發的兩種方式Python
- 兩種go傳送郵件的方式Go
- SpringBoot配置WebSocketSpring BootWeb
- SpringBoot引入WebSocketSpring BootWeb
- 聊聊 SpringBoot 中的兩種佔位符:@*@ 和 ${*}Spring Boot
- SpringBoot讀取配置資料的幾種方式Spring Boot
- 淺析SpringBoot載入配置的6種方式Spring Boot