當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Boot笔记-WebSocket的使用
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot笔记-WebSocket的使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
程序結(jié)構(gòu)如下:
關(guān)鍵源碼如下:
WebSocketConfig.java
package com.example.demo.config;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.server.standard.ServerEndpointExporter;@Configuration public class WebSocketConfig {@Beanpublic ServerEndpointExporter serverEndpointExporter() {return new ServerEndpointExporter();}}MyController.java
package com.example.demo.controller;import com.alibaba.fastjson.JSONObject; import com.example.demo.service.WsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController;@RestController public class MyController {@AutowiredWsService wsService;@PostMapping("/msg")public Object sendMsg(@RequestBody String req){JSONObject jsonObject = JSONObject.parseObject(req);System.out.println(jsonObject.getString("msg"));wsService.sendInfo(jsonObject.getString("msg"));return "success";} }WsService.java
package com.example.demo.service;import com.example.demo.websocket.WebSocketServer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service;@Service public class WsService {@Autowiredpublic WebSocketServer webSocketServer;@Asyncpublic void sendInfo(String message) {webSocketServer.sendInfo(message);} }WebSocketServer.java
package com.example.demo.websocket;import org.springframework.stereotype.Component;import javax.websocket.OnClose; import javax.websocket.OnError; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.PathParam; import javax.websocket.server.ServerEndpoint; import java.io.IOException; import java.util.HashMap;@Component @ServerEndpoint("/ws/{token}") public class WebSocketServer {//每個客戶端有一個tokenprivate String token = "";private static HashMap<String, Session> map = new HashMap<>();@OnOpenpublic void onOpen(Session session, @PathParam("token") String token){map.put(token, session);this.token = token;System.out.println("新連接:" + session);}@OnClosepublic void onClose(Session session){map.remove(this.token);System.out.println("連接關(guān)閉:" + session);}@OnErrorpublic void onError(Session session, Throwable error){System.out.println("錯誤:" + session + "," + error);}public void onMessage(String message, Session session){System.out.println("接收到消息:" + message);}//群發(fā)public void sendInfo(String message){for (String token : map.keySet()) {Session session = map.get(token);try {session.getBasicRemote().sendText(message);}catch (IOException e) {e.printStackTrace();}}} }application.properties
server.port=8880源碼打包下載地址:
https://github.com/fengfanchen/Java/tree/master/WebServiceDemo
?
?
?
總結(jié)
以上是生活随笔為你收集整理的Spring Boot笔记-WebSocket的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LinuxQt工作笔记-查看程序工作目录
- 下一篇: Spring Boot笔记-JPA分页(