博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用websocket实现后台推送消息
阅读量:5114 次
发布时间:2019-06-13

本文共 4657 字,大约阅读时间需要 15 分钟。

 

转:

1前台实现

1 connect:function() { 2         var webSocketIP = window.CRM_CONFIG.WebSocketIP; 3 var target = 'ws://'+webSocketIP+'/websocket'; 4 if ('WebSocket' in window) { 5 ws = new WebSocket(target); 6 } else if ('MozWebSocket' in window) { 7 ws = new MozWebSocket(target); 8 } else { 9 return; 10 } 11 ws.onopen = function () { 12 console.log('Info: WebSocket connection opened.'); 13 document.getElementById("wsMsg").style.display = "block"; 14 }; 15 var self = this; 16 ws.onmessage = function (event) { 17 if('您有如下工单需要及时处理:'!= event.data) { 18 self.setState({wsMessage: event.data}) 19 document.getElementById("wsMsg").style.display = "block"; 20 }else{ 21 document.getElementById("wsMsg").style.display = "none"; 22 } 23 }; 24 25 
26
27 {this.state.wsMessage} 28

2、pom.xml依赖

1     
2
3
javax.servlet
4
javax.servlet-api
5
3.1.0
6
7
8
org.springframework
9
spring-websocket
10
${spring.version}
11

3、spring.xml配置

4、java代码

package com.oasis.crm.controller.websocket;import com.oasis.crm.dao.biz.user.UserDao;import com.oasis.crm.model.biz.user.User; import com.oasis.crm.service.biz.order.AcceptedWorkOrderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; 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.Timer; import java.util.TimerTask; /** * 推送即将要处理完成的受理单给处理人 */ @RequestMapping("/websocket") public class WebsocketEndPoint extends TextWebSocketHandler { @Autowired private AcceptedWorkOrderService acceptedWorkOrderService; @Autowired private UserDao userDao; private Timer timer; @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { if(!session.isOpen()){ timer.cancel(); return; } super.handleTextMessage(session, message); session.sendMessage(message); } @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { String loginUserName = session.getPrincipal().getName(); User user = userDao.findUserByLoginName(loginUserName); timer = new Timer(true); long delay = 0; OrderTimeTask orderTimeTask = new OrderTimeTask(user,session); timer.schedule(orderTimeTask,delay, 60000);// 设定指定的时间time,此处为1分钟 } class OrderTimeTask extends TimerTask{ private User user; private WebSocketSession session; public OrderTimeTask(User user,WebSocketSession session){ this.user = user; this.session = session; } @Override public void run() { try { String reminder = acceptedWorkOrderService.getLastReminderOrder(user.getId()); TextMessage textMessage = new TextMessage(reminder); handleMessage(session,textMessage); } catch (Exception e){ e.printStackTrace(); } } } @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { System.out.println("Connection Closed!"); } }
package com.oasis.crm.controller.websocket;import org.springframework.http.server.ServerHttpRequest;import org.springframework.http.server.ServerHttpResponse; import org.springframework.web.socket.WebSocketHandler; import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor; import java.util.Map; public class HandshakeInterceptor extends HttpSessionHandshakeInterceptor { @Override public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map
attributes) throws Exception { return super.beforeHandshake(request, response, wsHandler, attributes); } @Override public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception ex) { super.afterHandshake(request, response, wsHandler, ex); } }

================

或者前台拉定时取消息

setInterval(()=> {            this.getReminders();        }, 300000); getReminders(){        $.getJSON(Remote.acceptedWorkOrder.reminderOrders,packVo=>{            this.setState({                wsMessage:packVo.vo            });        });        if('您有如下工单需要及时处理:'!= this.state.wsMessage&&''!=this.state.wsMessage){            document.getElementById("wsMsg").style.display = "block";        }else{            document.getElementById("wsMsg").style.display = "none";        }    }

 

转载于:https://www.cnblogs.com/libin6505/p/10432349.html

你可能感兴趣的文章
中文词频统计
查看>>
了解node.js
查看>>
想做移动开发,先看看别人怎么做
查看>>
Eclipse相关集锦
查看>>
虚拟化架构中小型机构通用虚拟化架构
查看>>
继承条款effecitve c++ 条款41-45
查看>>
HTML+CSS学习笔记(九)
查看>>
【BZOJ2286】【SDOI2011】消耗战 [虚树][树形DP]
查看>>
Java泛型的基本使用
查看>>
1076 Wifi密码 (15 分)
查看>>
rsync
查看>>
noip模拟赛 党
查看>>
bzoj2038 [2009国家集训队]小Z的袜子(hose)
查看>>
Java反射机制及其Class类浅析
查看>>
Postman-----如何导入和导出
查看>>
移动设备显示尺寸大全 CSS3媒体查询
查看>>
hihoCoder #1831 : 80 Days-RMQ (ACM/ICPC 2018亚洲区预选赛北京赛站网络赛)
查看>>
图片等比例缩放及图片上下剧中
查看>>
WebView加载网页详情
查看>>
【转载】Linux screen 命令详解
查看>>