用websocket实现后台推送消息

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    <div id="wsMsg" className="msgDialog msgDiv">
26                     <div className="msgContent"  ><a onClick ={this.closeMsg}> X&nbsp;</a></div>
27                     {this.state.wsMessage}
28                 </div>

2、pom.xml依赖

 1     <!--spring-websocket-->
 2         <dependency>
 3             <groupId>javax.servlet</groupId>
 4             <artifactId>javax.servlet-api</artifactId>
 5             <version>3.1.0</version>
 6         </dependency>
 7         <dependency>
 8             <groupId>org.springframework</groupId>
 9             <artifactId>spring-websocket</artifactId>
10             <version>${spring.version}</version>
11         </dependency>

3、spring.xml配置

 <!--websocket 配置-->
    <bean id="websocket" class="com.oasis.crm.controller.websocket.WebsocketEndPoint"/>
    <websocket:handlers  allowed-origins="*">
        <websocket:mapping path="/websocket" handler="websocket"/>
        <websocket:handshake-interceptors>
            <bean class="com.oasis.crm.controller.websocket.HandshakeInterceptor"/>
        </websocket:handshake-interceptors>
    </websocket:handlers>

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<String, Object> 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";
        }
    }
时间: 2024-07-28 18:45:55

用websocket实现后台推送消息的相关文章

Websocket web实时消息服务器后台推送技术方案---GoEasy

Goeasy, 它是一款第三方推送服务平台,使用它的API可以轻松搞定实时推送!个人感觉goeasy推送更稳定,推送速度快,代码简单易懂上手快浏览器兼容性:GoEasy推送支持websocket 和polling两种连接方式,从而可以支持IE6及其以上的所有版本,同时还支持其它浏览器诸如Firefox, Chrome, Safari 等等.支 持不同的开发语言:   GoEasy推送提供了Restful API接口,无论你的后台程序用的是哪种语言都可以通过RestfulAPI来实现后台实时推送.

Python web实时消息服务器后台推送技术方案---GoEasy

Goeasy, 它是一款第三方推送服务平台,使用它的API可以轻松搞定实时推送!个人感觉goeasy推送更稳定,推送速度快,代码简单易懂上手快浏览器兼容性:GoEasy推送支持websocket 和polling两种连接方式,从而可以支持IE6及其以上的所有版本,同时还支持其它浏览器诸如Firefox, Chrome, Safari 等等.支 持不同的开发语言:   GoEasy推送提供了Restful API接口,无论你的后台程序用的是哪种语言都可以通过RestfulAPI来实现后台实时推送.

C(++) web实时消息服务器后台推送技术方案---GoEasy

Goeasy, 它是一款第三方推送服务平台,使用它的API可以轻松搞定实时推送!个人感觉goeasy推送更稳定,推送速度快,代码简单易懂上手快浏览器兼容性:GoEasy推送支持websocket 和polling两种连接方式,从而可以支持IE6及其以上的所有版本,同时还支持其它浏览器诸如Firefox, Chrome, Safari 等等.支 持不同的开发语言:   GoEasy推送提供了Restful API接口,无论你的后台程序用的是哪种语言都可以通过RestfulAPI来实现后台实时推送.

node.js web实时消息服务器后台推送技术方案---GoEasy

Goeasy, 它是一款第三方推送服务平台,使用它的API可以轻松搞定实时推送!个人感觉goeasy推送更稳定,推送速度快,代码简单易懂上手快浏览器兼容性:GoEasy推送支持websocket 和polling两种连接方式,从而可以支持IE6及其以上的所有版本,同时还支持其它浏览器诸如Firefox, Chrome, Safari 等等.支 持不同的开发语言:   GoEasy推送提供了Restful API接口,无论你的后台程序用的是哪种语言都可以通过RestfulAPI来实现后台实时推送.

PHP web实时消息服务器后台推送技术方案---GoEasy

Goeasy, 它是一款第三方推送服务平台,使用它的API可以轻松搞定实时推送!个人感觉goeasy推送更稳定,推送速度快,代码简单易懂上手快浏览器兼容性:GoEasy推送支持websocket 和polling两种连接方式,从而可以支持IE6及其以上的所有版本,同时还支持其它浏览器诸如Firefox, Chrome, Safari 等等.支 持不同的开发语言:   GoEasy推送提供了Restful API接口,无论你的后台程序用的是哪种语言都可以通过RestfulAPI来实现后台实时推送.

JAVA web实时消息服务器后台推送技术方案---GoEasy

Goeasy, 它是一款第三方推送服务平台,使用它的API可以轻松搞定实时推送!个人感觉goeasy推送更稳定,推送速度快,代码简单易懂上手快浏览器兼容性:GoEasy推送支持websocket 和polling两种连接方式,从而可以支持IE6及其以上的所有版本,同时还支持其它浏览器诸如Firefox, Chrome, Safari 等等.支 持不同的开发语言:   GoEasy推送提供了Restful API接口,无论你的后台程序用的是哪种语言都可以通过RestfulAPI来实现后台实时推送.

Ruby web实时消息服务器后台推送技术方案---GoEasy

Goeasy, 它是一款第三方推送服务平台,使用它的API可以轻松搞定实时推送!个人感觉goeasy推送更稳定,推送速度快,代码简单易懂上手快浏览器兼容性:GoEasy推送支持websocket 和polling两种连接方式,从而可以支持IE6及其以上的所有版本,同时还支持其它浏览器诸如Firefox, Chrome, Safari 等等.支 持不同的开发语言:   GoEasy推送提供了Restful API接口,无论你的后台程序用的是哪种语言都可以通过RestfulAPI来实现后台实时推送.

使用Java后台服务器通过腾讯信鸽向app推送消息_快速上手案例

这两天公司业务有需求,当用户进行一些操作,比如下订单.支付等操作时,需要关联后台自动向app推送顶栏消息.为了实现这个功能,使用了腾讯信鸽推送.<腾讯信鸽推送>已经封装好了推送代码,只需要调用它一个方法就可以,在方法参数中传入识别id和推送内容,就能完成推送消息到目标设备,实现起来其实非常容易! 第一步:登陆腾讯信鸽网站,下载jar包http://xg.qq.com/docs/server_api/other.html 然后点击java服务端,弹出下载框(我用的是火狐浏览器),保存文件 解压文

springboot 项目==基于websocket的服务端推送消息。

1.创建springboot项目,首先我们还是先引入依赖 <!-- webSocket begin--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId></dependency><!-- webSocket end--> 2.创建配置类