pushlet
原帖地址:http://blog.csdn.net/meikidd/article/details/7446778
最近项目中有服务器端推送的需求,考察了一下,感觉pushlet比较适合我们的情况。
用起来比较简单,网上资料也比较多(参考:开源框架Pushlet入门),就不多费笔墨了。
最常见的用法如下:
[java] view plaincopy
- package com.ljq.test;
- import java.io.serializable;
- import nl.justobjects.pushlet.core.Event;
- import nl.justobjects.pushlet.core.EventPullSource;
- @SuppressWarnings("serial")
- public class HelloWorldPlushlet extends EventPullSource implements Serializable {
- /**
- * 设置休眠时间
- */
- @Override
- protected long getSleepTime() {
- return 1000;
- }
- /**
- * 创建事件
- *
- * 业务部分写在pullEvent()方法中,这个方法会被定时调用。
- */
- @Override
- protected Event pullEvent() {
- Event event = Event.createDataEvent("/linjiqin/hw");
- event.setfield("hw", "HelloWorld!!!!");
- return event;
- }
- }
在使用的过程中发现,如果要在pullEvent()方法中获取参数比较麻烦,看了半天官方文档也没有找到好的方法(也可能是我没有找对地方,总感觉pushlet不会有这种问题,如果你们知道请一定告诉我)。只好去看源代码,发现在nl.justobjects.pushlet.servlet.Pushlet中已经将request参数传进了session(注意是nl.justobjects.pushlet.core.Session)。但是在session构造的时候没有用到request。看到这里,就大概知道改怎么改了。代码如下:
1. nl.justobjects.pushlet.core.Session,添加了event域和getEvent()方法,修改了public static Session create(String anId, Event anEvent)方法
[java] view plaincopy
- public class Session implements Protocol, ConfigDefs {
- private controller controller;
- private subscriber subscriber;
- private Event event;
- ...
- /**
- * Create instance through factory method.
- *
- * @param anId a session id
- * @return a Session object (or derived)
- * @throws PushletException exception, usually misconfiguration
- */
- public static Session create(String anId, Event anEvent) throws PushletException {
- Session session;
- try {
- session = (Session) Config.getClass(SESSION_CLASS, "nl.justobjects.pushlet.core.Session").newInstance();
- } catch (throwable t) {
- throw new PushletException("cannot instantiate Session from config", t);
- }
- // Init session
- session.id = anId;
- session.controller = Controller.create(session);
- session.subscriber = Subscriber.create(session);
- session.event = anEvent;
- return session;
- }
- ...
- /**
- * Return event.
- */
- public Event getEvent() {
- return event;
- }
- ...
- }
[java] view plaincopy
- /**
- * Create new Session (but add later).
- */
- public Session createSession(Event anEvent) throws PushletException {
- // Trivial
- return Session.create(createSessionId(), anEvent);
- }
3. ajax-pushlet-client.js,PL添加了parameters属性,修改了_doRequest函数,在函数的最后加了如下一段:
[JavaScript] view plaincopy
- if(PL.parameters.length > 0) {
- for (var i = 0; i < PL.parameters.length; i++) {
- var para = PL.parameters[i];
- url += "&" + para.name + "=" + para.value;
- }
- }
修改后的ajax-pushlet-client.js -_doRequest()函数:
[javascript] view plaincopy
- _doRequest: function(anEvent, aQuery) {
- ...
- // Construct base URL for GET
- var url = PL.pushletURL + '?p_event=' + anEvent;
- // optionally attach query string
- if (aQuery) {
- url = url + '&' + aQuery;
- }
- // Optionally attach session id
- if (PL.sessionId != null) {
- url = url + '&p_id=' + PL.sessionId;
- if (anEvent == 'p_leave') {
- PL.sessionId = null;
- }
- }
- if(PL.parameters.length > 0) {
- for (var i = 0; i < PL.parameters.length; i++) {
- var para = PL.parameters[i];
- url += "&" + para.name + "=" + para.value;
- }
- }
- PL.debug('_doRequest', url);
- PL._getXML(url, PL._onresponse);
- },
好了,源代码修改完毕,下面是一个如何传递参数的例子
在页面上js代码:
[javascript] view plaincopy
- // pushlet服务器推送,更新实时监控模块
- var initPushlet = function() {
- PL.parameters.push({"name":"user-id", "value":"001");
- PL._init();
- PL.joinListen('/source/event');
- };
在HelloWorldPlushlet的pullEvent()方法调用:
[java] view plaincopy
- Session[] sessions = SessionManager.getInstance().getSessions();
- String userId = sessions[0].getEvent().getField("user-id");
相关阅读
@RequestBody,415Unsupported Media Type错误,真正有用
用了一上午查找问题解决方案,网上的试验了一遍都没用,最后终于解决~我的答案在最后 问题:前端传json,后端也返回json,出现格式不匹配报
1.前言 本实例需要用到的python包有requests、PrettyTable(用于打印展示成表格形式) pip install requests pip install pretty
produces在@requestMapping中的使用方式和作用
produces可能不算一个注解,因为什么呢,它是注解@requestMapping注解里面的属性项, 它的作用是指定返回值类型,不但可以设置返回值类型
携程eleven_requests声明:本文仅作技术交流,严禁用于任何非法用途(如有冒犯,请联系我删除此文)1.写在前面:2.eleven步骤分析1)寻找思路2)
HttpServletRequest参数获取,HttpServletRequest详解
--------------------------HttpServletRequest参数获取,HttpServletRequest详解--------------------------------- HttpServletR