赞
踩
上代码
pom.xml导入的包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
WebSoketConfig
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Component
public class WebSoketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter(){
return new ServerEndpointExporter();
}
}
后端Util
import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import javax.websocket.OnClose; import javax.websocket.OnMessage; 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.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArraySet; @Component @ServerEndpoint("/webSocket/{uId}") @Slf4j public class WebSocketServerUtil { private Session session; private static CopyOnWriteArraySet<WebSocketServerUtil > webSocketSet = new CopyOnWriteArraySet<>(); private static ConcurrentHashMap<Long,WebSocketServerUtil > webSocketMap = new ConcurrentHashMap<>(); private Long uId = null; @OnOpen public void onOpen(Session session, @PathParam("uId") Long uId){ this.session = session; this.uId = uId; if(webSocketMap .containsKey(uId)){ webSocketMap .remove(uId); webSocketMap .put(uId,this); }else{ webSocketMap .put(uId,this); webSocketSet.add(this); } log.info("【websocket消息】有新的连接,总数:{}",webSocketMap.size()); } @OnClose public void onClose(){ if(webSocketMap.containsKey(uId)){ webSocketMap.remove(uId); //从set中删除 webSocketSet.remove(this); } log.info("【websocket消息】连接断开,总数:{}",webSocketSet.size()); } @OnMessage public void onMessage(String message){ log.info("【websocket消息】收到客户端发来的消息:{}",message); } public void sendMessage(String message){ try { this.session.getBasicRemote().sendText(message); } catch (IOException e) { e.printStackTrace(); } } /** * 发送自定义消息 * */ public static void sendInfo(String message,Long uId) throws Exception { //log.info("发送消息到:"+uId+",报文:"+message); if(webSocketMap.containsKey(uId)){ webSocketMap.get(uId).sendMessage(message); }else{ log.error("用户"+uId+",不在线!"); throw new Exception("连接已关闭,请刷新页面后重试"); } } }
后端消息推送
Long uId = new Long("1");
Map msgMap = new HashMap();
msgMap.put("step",1);
msgMap.put("type",2);
msgMap.put("msg","hello");
WebSocketServerUtil.sendInfo(JsonUtil.toJson(msgMap),uId);
前端JS
/** * 初始化websocket连接 */ function initWebSocket() { let uId = 1; var websocket = null; if('WebSocket' in window) { websocket = new WebSocket("ws://localhost:8009/webSocket"+uId ); } else { alert("该浏览器不支持websocket!"); } websocket.onopen = function(event) { console.log("建立连接"); websocket.send('Hello WebSockets!'); } websocket.onclose = function(event) { console.log('连接关闭') reconnect(); //尝试重连websocket } //建立通信后,监听到后端的数据传递 websocket.onmessage = function(event) { let data = JSON.parse(event.data); //业务处理.... if(data.step == 1){ alert(data.msg); } } websocket.onerror = function() { // notify.warn("websocket通信发生错误!"); // initWebSocket() } window.onbeforeunload = function() { websocket.close(); } // 重连 function reconnect() { console.log("正在重连"); // 进行重连 setTimeout(function () { initWebSocket(); }, 1000); }
看下效果
如果以上内容对小伙伴有帮助,请关注支持,如有疑问可私信我,欢迎指教!
创作不易如有打赏,感激不尽
您的支持是我创作的动力!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。