日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringMVC整合websocket实现消息推送及触发

發(fā)布時間:2023/12/20 javascript 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringMVC整合websocket实现消息推送及触发 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標準>>>

1.創(chuàng)建websocket握手協(xié)議的后臺

(1)HandShake的實現(xiàn)類

?

[java]?view plain?copy

  • /**?
  • ?*Project?Name:?price?
  • ?*File?Name:????HandShake.java?
  • ?*Package?Name:?com.yun.websocket?
  • ?*Date:?????????2016年9月3日?下午4:44:27?
  • ?*Copyright?(c)?2016,578888218@qq.com?All?Rights?Reserved.?
  • */??
  • ??
  • package?com.yun.websocket;??
  • ??
  • import?java.util.Map;??
  • ??
  • import?org.springframework.http.server.ServerHttpRequest;??
  • import?org.springframework.http.server.ServerHttpResponse;??
  • import?org.springframework.http.server.ServletServerHttpRequest;??
  • import?org.springframework.web.socket.WebSocketHandler;??
  • import?org.springframework.web.socket.server.HandshakeInterceptor;??
  • ??
  • /**?
  • ?*Title:??????HandShake<br/>?
  • ?*Description:?
  • ?*@Company:???青島勵圖高科<br/>?
  • ?*@author:????劉云生?
  • ?*@version:???v1.0?
  • ?*@since:?????JDK?1.7.0_80?
  • ?*@Date:??????2016年9月3日?下午4:44:27?<br/>?
  • */??
  • public?class?HandShake?implements?HandshakeInterceptor{??
  • ??
  • ????@Override??
  • ????public?boolean?beforeHandshake(ServerHttpRequest?request,?ServerHttpResponse?response,?WebSocketHandler?wsHandler,??
  • ????????????Map<String,?Object>?attributes)?throws?Exception?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ????????String?jspCode?=?((ServletServerHttpRequest)?request).getServletRequest().getParameter("jspCode");??
  • ????????//?標記用戶??
  • ????????//String?userId?=?(String)?session.getAttribute("userId");??
  • ????????if(jspCode!=null){??
  • ????????????attributes.put("jspCode",?jspCode);??
  • ????????}else{??
  • ????????????return?false;??
  • ????????}??
  • ????????return?true;??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?afterHandshake(ServerHttpRequest?request,?ServerHttpResponse?response,?WebSocketHandler?wsHandler,??
  • ????????????Exception?exception)?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ??????????
  • ????}??
  • ??
  • }??
  • ?

    ?

    (2)MyWebSocketConfig的實現(xiàn)類

    ?

    [java]?view plain?copy

  • /**?
  • ?*Project?Name:?price?
  • ?*File?Name:????MyWebSocketConfig.java?
  • ?*Package?Name:?com.yun.websocket?
  • ?*Date:?????????2016年9月3日?下午4:52:29?
  • ?*Copyright?(c)?2016,578888218@qq.com?All?Rights?Reserved.?
  • */??
  • ??
  • package?com.yun.websocket;??
  • ??
  • import?javax.annotation.Resource;??
  • ??
  • import?org.springframework.stereotype.Component;??
  • import?org.springframework.web.servlet.config.annotation.EnableWebMvc;??
  • import?org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;??
  • import?org.springframework.web.socket.config.annotation.EnableWebSocket;??
  • import?org.springframework.web.socket.config.annotation.WebSocketConfigurer;??
  • import?org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;??
  • ??
  • /**?
  • ?*Title:??????MyWebSocketConfig<br/>?
  • ?*Description:?
  • ?*@Company:???青島勵圖高科<br/>?
  • ?*@author:????劉云生?
  • ?*@version:???v1.0?
  • ?*@since:?????JDK?1.7.0_80?
  • ?*@Date:??????2016年9月3日?下午4:52:29?<br/>?
  • */??
  • @Component??
  • @EnableWebMvc??
  • @EnableWebSocket??
  • public?class?MyWebSocketConfig?extends?WebMvcConfigurerAdapter?implements?WebSocketConfigurer{??
  • ???@Resource??
  • ????MyWebSocketHandler?handler;??
  • ??????
  • ????@Override??
  • ????public?void?registerWebSocketHandlers(WebSocketHandlerRegistry?registry)?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ????????registry.addHandler(handler,?"/wsMy").addInterceptors(new?HandShake());??
  • ????????registry.addHandler(handler,?"/wsMy/sockjs").addInterceptors(new?HandShake()).withSockJS();??
  • ????}??
  • ??
  • }??
  • ?

    (3)MyWebSocketHandler的實現(xiàn)類

    ?

    [java]?view plain?copy

  • /**?
  • ?*Project?Name:?price?
  • ?*File?Name:????MyWebSocketHandler.java?
  • ?*Package?Name:?com.yun.websocket?
  • ?*Date:?????????2016年9月3日?下午4:55:12?
  • ?*Copyright?(c)?2016,578888218@qq.com?All?Rights?Reserved.?
  • */??
  • ??
  • package?com.yun.websocket;??
  • ??
  • import?java.io.IOException;??
  • import?java.util.HashMap;??
  • import?java.util.Iterator;??
  • import?java.util.Map;??
  • import?java.util.Map.Entry;??
  • ??
  • import?org.springframework.stereotype.Component;??
  • import?org.springframework.web.socket.CloseStatus;??
  • import?org.springframework.web.socket.TextMessage;??
  • import?org.springframework.web.socket.WebSocketHandler;??
  • import?org.springframework.web.socket.WebSocketMessage;??
  • import?org.springframework.web.socket.WebSocketSession;??
  • ??
  • import?com.google.gson.GsonBuilder;??
  • ??
  • /**?
  • ?*Title:??????MyWebSocketHandler<br/>?
  • ?*Description:?
  • ?*@Company:???青島勵圖高科<br/>?
  • ?*@author:????劉云生?
  • ?*@version:???v1.0?
  • ?*@since:?????JDK?1.7.0_80?
  • ?*@Date:??????2016年9月3日?下午4:55:12?<br/>?
  • */??
  • @Component??
  • public?class?MyWebSocketHandler?implements?WebSocketHandler{??
  • ??
  • ????public?static?final?Map<String,?WebSocketSession>?userSocketSessionMap;??
  • ??
  • ????static?{??
  • ????????userSocketSessionMap?=?new?HashMap<String,?WebSocketSession>();??
  • ????}??
  • ??????
  • ??????
  • ????@Override??
  • ????public?void?afterConnectionEstablished(WebSocketSession?session)?throws?Exception?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ????????String?jspCode?=?(String)?session.getHandshakeAttributes().get("jspCode");??
  • ????????if?(userSocketSessionMap.get(jspCode)?==?null)?{??
  • ????????????userSocketSessionMap.put(jspCode,?session);??
  • ????????}??
  • ????????for(int?i=0;i<10;i++){??
  • ????????????//broadcast(new?TextMessage(new?GsonBuilder().create().toJson("\"number\":\""+i+"\"")));??
  • ????????????session.sendMessage(new?TextMessage(new?GsonBuilder().create().toJson("\"number\":\""+i+"\"")));??
  • ????????}??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?handleMessage(WebSocketSession?session,?WebSocketMessage<?>?message)?throws?Exception?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ????????//Message?msg=new?Gson().fromJson(message.getPayload().toString(),Message.class);??
  • ????????//msg.setDate(new?Date());??
  • //??????sendMessageToUser(msg.getTo(),?new?TextMessage(new?GsonBuilder().setDateFormat("yyyy-MM-dd?HH:mm:ss").create().toJson(msg)));??
  • ??????????
  • ????????session.sendMessage(message);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?handleTransportError(WebSocketSession?session,?Throwable?exception)?throws?Exception?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ????????if?(session.isOpen())?{??
  • ????????????session.close();??
  • ????????}??
  • ????????Iterator<Entry<String,?WebSocketSession>>?it?=?userSocketSessionMap??
  • ????????????????.entrySet().iterator();??
  • ????????//?移除Socket會話??
  • ????????while?(it.hasNext())?{??
  • ????????????Entry<String,?WebSocketSession>?entry?=?it.next();??
  • ????????????if?(entry.getValue().getId().equals(session.getId()))?{??
  • ????????????????userSocketSessionMap.remove(entry.getKey());??
  • ????????????????System.out.println("Socket會話已經(jīng)移除:用戶ID"?+?entry.getKey());??
  • ????????????????break;??
  • ????????????}??
  • ????????}??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?afterConnectionClosed(WebSocketSession?session,?CloseStatus?closeStatus)?throws?Exception?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ????????System.out.println("Websocket:"?+?session.getId()?+?"已經(jīng)關(guān)閉");??
  • ????????Iterator<Entry<String,?WebSocketSession>>?it?=?userSocketSessionMap??
  • ????????????????.entrySet().iterator();??
  • ????????//?移除Socket會話??
  • ????????while?(it.hasNext())?{??
  • ????????????Entry<String,?WebSocketSession>?entry?=?it.next();??
  • ????????????if?(entry.getValue().getId().equals(session.getId()))?{??
  • ????????????????userSocketSessionMap.remove(entry.getKey());??
  • ????????????????System.out.println("Socket會話已經(jīng)移除:用戶ID"?+?entry.getKey());??
  • ????????????????break;??
  • ????????????}??
  • ????????}??
  • ????}??
  • ??
  • ????@Override??
  • ????public?boolean?supportsPartialMessages()?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ????????return?false;??
  • ????}??
  • ????/**?
  • ?????*?群發(fā)?
  • ?????*?@Title:???????broadcast???
  • ?????*?@Description:?TODO???
  • ?????*?@param:???????@param?message?
  • ?????*?@param:???????@throws?IOException?
  • ?????*?@return:??????void?
  • ?????*?@author:??????劉云生?
  • ?????*?@Date:????????2016年9月10日?下午4:23:30????
  • ?????*?@throws?
  • ?????*/??
  • ????public?void?broadcast(final?TextMessage?message)?throws?IOException?{??
  • ????????Iterator<Entry<String,?WebSocketSession>>?it?=?userSocketSessionMap??
  • ????????????????.entrySet().iterator();??
  • ??
  • ????????//?多線程群發(fā)??
  • ????????while?(it.hasNext())?{??
  • ??
  • ????????????final?Entry<String,?WebSocketSession>?entry?=?it.next();??
  • ??
  • ????????????if?(entry.getValue().isOpen())?{??
  • ????????????????new?Thread(new?Runnable()?{??
  • ??
  • ????????????????????public?void?run()?{??
  • ????????????????????????try?{??
  • ????????????????????????????if?(entry.getValue().isOpen())?{??
  • ????????????????????????????????entry.getValue().sendMessage(message);??
  • ????????????????????????????}??
  • ????????????????????????}?catch?(IOException?e)?{??
  • ????????????????????????????e.printStackTrace();??
  • ????????????????????????}??
  • ????????????????????}??
  • ??
  • ????????????????}).start();??
  • ????????????}??
  • ??
  • ????????}??
  • ????}??
  • ??????
  • ????/**?
  • ?????*?給所有在線用戶的實時工程檢測頁面發(fā)送消息?
  • ?????*??
  • ?????*?@param?message?
  • ?????*?@throws?IOException?
  • ?????*/??
  • ????public?void?sendMessageToJsp(final?TextMessage?message,String?type)?throws?IOException?{??
  • ????????Iterator<Entry<String,?WebSocketSession>>?it?=?userSocketSessionMap??
  • ????????????????.entrySet().iterator();??
  • ??
  • ????????//?多線程群發(fā)??
  • ????????while?(it.hasNext())?{??
  • ??
  • ????????????final?Entry<String,?WebSocketSession>?entry?=?it.next();??
  • ????????????if?(entry.getValue().isOpen()?&&?entry.getKey().contains(type))?{??
  • ????????????????new?Thread(new?Runnable()?{??
  • ??
  • ????????????????????public?void?run()?{??
  • ????????????????????????try?{??
  • ????????????????????????????if?(entry.getValue().isOpen())?{??
  • ????????????????????????????????entry.getValue().sendMessage(message);??
  • ????????????????????????????}??
  • ????????????????????????}?catch?(IOException?e)?{??
  • ????????????????????????????e.printStackTrace();??
  • ????????????????????????}??
  • ????????????????????}??
  • ??
  • ????????????????}).start();??
  • ????????????}??
  • ??
  • ????????}??
  • ????}??
  • }??

  • ?

    2.創(chuàng)建websocket握手處理的前臺

    ?

    [javascript]?view plain?copy

  • <script>??
  • ????var?path?=?'<%=basePath%>';??
  • ????var?userId?=?'lys';??
  • ????if(userId==-1){??
  • ????????window.location.href="<%=basePath2%>";??
  • ????}??
  • ????var?jspCode?=?userId+"_AAA";??
  • ????var?websocket;??
  • ????if?('WebSocket'?in?window)?{??
  • ????????websocket?=?new?WebSocket("ws://"?+?path?+?"wsMy?jspCode="?+?jspCode);??
  • ????}?else?if?('MozWebSocket'?in?window)?{??
  • ????????websocket?=?new?MozWebSocket("ws://"?+?path?+?"wsMy?jspCode="?+?jspCode);??
  • ????}?else?{??
  • ????????websocket?=?new?SockJS("http://"?+?path?+?"wsMy/sockjs?jspCode="?+?jspCode);??
  • ????}??
  • ????websocket.onopen?=?function(event)?{??
  • ????????console.log("WebSocket:已連接");??
  • ????????console.log(event);??
  • ????};??
  • ????websocket.onmessage?=?function(event)?{??
  • ????????var?data?=?JSON.parse(event.data);??
  • ????????console.log("WebSocket:收到一條消息-norm",?data);??
  • ????????alert("WebSocket:收到一條消息");??
  • ????};??
  • ????websocket.onerror?=?function(event)?{??
  • ????????console.log("WebSocket:發(fā)生錯誤?");??
  • ????????console.log(event);??
  • ????};??
  • ????websocket.onclose?=?function(event)?{??
  • ????????console.log("WebSocket:已關(guān)閉");??
  • ????????console.log(event);??
  • ????}??
  • </script>??

  • ?

    3.通過Controller調(diào)用進行websocket的后臺推送

    ?

    [java]?view plain?copy

  • /**?
  • ?*Project?Name:?price?
  • ?*File?Name:????GarlicPriceController.java?
  • ?*Package?Name:?com.yun.price.garlic.controller?
  • ?*Date:?????????2016年6月23日?下午3:23:46?
  • ?*Copyright?(c)?2016,578888218@qq.com?All?Rights?Reserved.?
  • */??
  • ??
  • package?com.yun.price.garlic.controller;??
  • ??
  • import?java.io.IOException;??
  • import?java.util.Date;??
  • import?java.util.List;??
  • ??
  • import?javax.annotation.Resource;??
  • import?javax.servlet.http.HttpServletRequest;??
  • import?javax.servlet.http.HttpSession;??
  • ??
  • import?org.springframework.beans.factory.annotation.Autowired;??
  • import?org.springframework.stereotype.Controller;??
  • import?org.springframework.web.bind.annotation.RequestMapping;??
  • import?org.springframework.web.bind.annotation.RequestMethod;??
  • import?org.springframework.web.bind.annotation.ResponseBody;??
  • import?org.springframework.web.context.request.RequestContextHolder;??
  • import?org.springframework.web.context.request.ServletRequestAttributes;??
  • import?org.springframework.web.servlet.ModelAndView;??
  • import?org.springframework.web.socket.TextMessage;??
  • ??
  • import?com.google.gson.GsonBuilder;??
  • import?com.yun.common.entity.DataGrid;??
  • import?com.yun.price.garlic.dao.entity.GarlicPrice;??
  • import?com.yun.price.garlic.model.GarlicPriceModel;??
  • import?com.yun.price.garlic.service.GarlicPriceService;??
  • import?com.yun.websocket.MyWebSocketHandler;??
  • ??
  • /**?
  • ?*?Title:?GarlicPriceController<br/>?
  • ?*?Description:?
  • ?*??
  • ?*?@Company:?青島勵圖高科<br/>?
  • ?*?@author:?劉云生?
  • ?*?@version:?v1.0?
  • ?*?@since:?JDK?1.7.0_80?
  • ?*?@Date:?2016年6月23日?下午3:23:46?<br/>?
  • ?*/??
  • @Controller??
  • public?class?GarlicPriceController?{??
  • ????@Resource??
  • ????MyWebSocketHandler?myWebSocketHandler;??
  • ????@RequestMapping(value?=?"GarlicPriceController/testWebSocket",?method?={RequestMethod.POST,RequestMethod.GET},?produces?=?"application/json;?charset=utf-8")??
  • ????@ResponseBody??
  • ????public?String?testWebSocket()?throws?IOException{??
  • ????????myWebSocketHandler.sendMessageToJsp(new?TextMessage(new?GsonBuilder().create().toJson("\"number\":\""+"GarlicPriceController/testWebSocket"+"\"")),?"AAA");??
  • ????????return?"1";??
  • ????}??
  • ??????
  • }??

  • ?

    4.所用到的jar包

    [html]?view plain?copy

  • <dependency>??
  • ????????<groupId>org.springframework</groupId>??
  • ????????<artifactId>spring-websocket</artifactId>??
  • ????????<version>4.0.1.RELEASE</version>??
  • </dependency>????
  • ?

    5.運行的環(huán)境

    至少tomcat8.0以上版本,否則可能報錯

    ?

    原博文:http://blog.csdn.net/liuyunshengsir/article/details/52495919

    轉(zhuǎn)載于:https://my.oschina.net/u/3576011/blog/1559057

    總結(jié)

    以上是生活随笔為你收集整理的SpringMVC整合websocket实现消息推送及触发的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。