zoukankan      html  css  js  c++  java
  • SSH集成Pushlet

    首先是需要的资源:

    1)、pushlet.jar

    2)、ajax-pushlet-client.js

    第一步、web.xml配置servlet

      <servlet>
        <servlet-name>pushlet</servlet-name>
        <servlet-class>com.newgsn.tvmall.pushlet.servlet.Pushlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>pushlet</servlet-name>
        <url-pattern>/pushlet.srv</url-pattern>
      </servlet-mapping>

    Pushlet类自定义类,继承自nl.justobjects.pushlet.servlet.Pushlet.java
    第二步、创建自定义的Pushlet类

    初始化pushlet,并注入applicationContext

    public class Pushlet extends nl.justobjects.pushlet.servlet.Pushlet {
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        
        public void init() throws ServletException {
            super.init();
            
            ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
            SessionManager.getInstance().initApplicationContext(ctx);
        }
    
    }

    第三步、创建推送消息工具类

    public class PushletUtil {
        
        private static String PLATFORM_MESSAGE = "UNI_CAST_MESSAGE";
        
        /**
         * 发送消息至客户端
         * @param sessionId
         * @param msg
         */
        public static void sendMessage(String sessionId, String msg){
            if(StringUtils.isNotBlank(sessionId) && SessionManager.getInstance().hasSession(sessionId)){
                Event event = Event.createDataEvent("message");
                event.setField(PLATFORM_MESSAGE, msg);
                Dispatcher.getInstance().unicast(event, sessionId);
            }
        }
        
        /**
         * 广播至所有在线客户端
         * @param msg
         */
        public static void sendBroadcast(String msg){
            Event event = Event.createDataEvent("message");
            event.setField(PLATFORM_MESSAGE, msg);
            Dispatcher.getInstance().broadcast(event);
        }
    }

    实现点对点推送和广播推送
    第四步、jsp页面接收消息

    PL._init();
    PL.userId='qinfx';  
    p_join_listen("message");
    function onData(event) {   
        if(event.get('UNI_CAST_MESSAGE') != undefined ) {
            alert(event.get('UNI_CAST_MESSAGE'));    
        }
        if(event.get('UNI_LOGOFF') != undefined ) {
            alert(event.get('UNI_LOGOFF'));
        }
    }

    PL.userId为当前登录用户的id

  • 相关阅读:
    H5页面跳到安卓APP和iosAPP
    JS location.href传参及接受参数
    获取当前日期及对应星期
    前端获取当前一周时间 数组形式
    Java基础(四) Object 数组转成 String 数组
    定时任务cron表达式详解
    jquery如何删除数组中的一个元素?
    Mybatis Mapper.xml 需要查询返回List<String>
    oracle的 listagg() WITHIN GROUP () 行转列函数的使用
    如何修改Oracle中表的字段长度?
  • 原文地址:https://www.cnblogs.com/siyu/p/3811454.html
Copyright © 2011-2022 走看看