pushlet 傳遞頁面request參數

pushlet 傳遞頁面request參數
最近項目中有服務器端推送的需求,考察了一下,感覺pushlet比較適合我們的情況。

用起來比較簡單,網上資料也比較多(參考:開源框架Pushlet入門),就不多費筆墨了。


最常見的用法如下:


  1. package com.ljq.test;  

  2. import java.io.Serializable;  

  3. import nl.justobjects.pushlet.core.Event;  

  4. import nl.justobjects.pushlet.core.EventPullSource;  

  5. @SuppressWarnings("serial")  

  6. publicclass HelloWorldPlushlet extends EventPullSource implements Serializable {  

  7. /**

  8.     * 設置休眠時間

  9.     */

  10. @Override

  11. protectedlong getSleepTime() {  

  12. return1000;  

  13.    }  

  14. /**

  15.     * 創建事件

  16.     *

  17.     * 業務部分寫在pullEvent()方法中,這個方法會被定時調用。

  18.     */

  19. @Override

  20. protected Event pullEvent() {  

  21.        Event event = Event.createDataEvent("/linjiqin/hw");  

  22.        event.setField("hw", "HelloWorld!!!!");  

  23. return event;  

  24.    }  

  25. }  




在使用的過程中發現,如果要在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)方法


  1. publicclass Session implements Protocol, ConfigDefs {  

  2. private Controller controller;  

  3. private Subscriber subscriber;  

  4. private Event event;  

  5.    ...  

  6. /**

  7.     * Create instance through factory method.

  8.     *

  9.     * @param anId a session id

  10.     * @return a Session object (or derived)

  11.     * @throws PushletException exception, usually misconfiguration

  12.     */

  13. publicstatic Session create(String anId, Event anEvent) throws PushletException {  

  14.        Session session;  

  15. try {  

  16.            session = (Session) Config.getClass(SESSION_CLASS, "nl.justobjects.pushlet.core.Session").newInstance();  

  17.        } catch (Throwable t) {  

  18. thrownew PushletException("Cannot instantiate Session from config", t);  

  19.        }  

  20. // Init session

  21.        session.id = anId;  

  22.        session.controller = Controller.create(session);  

  23.        session.subscriber = Subscriber.create(session);  

  24.        session.event = anEvent;  

  25. return session;  

  26.    }  

  27.    ...  

  28. /**

  29.     * Return event.

  30.     */

  31. public Event getEvent() {  

  32. return event;  

  33.    }  

  34.    ...  

  35. }  

2. nl.justobjects.pushlet.core.SessionManager,修改了createSession()方法



  1. /**

  2. * Create new Session (but add later).

  3. */

  4. public Session createSession(Event anEvent) throws PushletException {  

  5. // Trivial

  6. return Session.create(createSessionId(), anEvent);  

  7. }  

3. ajax-pushlet-client.js,PL添加了parameters屬性,修改了_doRequest函數,在函數的最後加了如下一段:


[javascript]view plaincopy
  1. if(PL.parameters.length > 0) {  

  2. for (var i = 0; i < PL.parameters.length; i++) {  

  3. var para = PL.parameters[i];  

  4.        url += "&" + para.name + "=" + para.value;  

  5.    }  

  6. }  


修改後的ajax-pushlet-client.js -_doRequest()函數:




[javascript]view plaincopy
  1. _doRequest: function(anEvent, aQuery) {  

  2.        ...  

  3. // Construct base URL for GET

  4. var url = PL.pushletURL + '?p_event=' + anEvent;  

  5. // Optionally attach query string

  6. if (aQuery) {  

  7.            url = url + '&' + aQuery;  

  8.        }  

  9. // Optionally attach session id

  10. if (PL.sessionId != null) {  

  11.            url = url + '&p_id=' + PL.sessionId;  

  12. if (anEvent == 'p_leave') {  

  13.                PL.sessionId = null;  

  14.            }  

  15.        }  

  16. if(PL.parameters.length > 0) {  

  17. for (var i = 0; i < PL.parameters.length; i++) {  

  18. var para = PL.parameters[i];  

  19.                url += "&" + para.name + "=" + para.value;  

  20.            }  

  21.        }  

  22.        PL.debug('_doRequest', url);  

  23.        PL._getXML(url, PL._onResponse);  

  24.    },  


好了,源代碼修改完畢,下面是一個如何傳遞參數的例子

在頁面上js代碼:


[javascript]view plaincopy
  1. // pushlet服務器推送,更新實時監控模塊

  2. var initPushlet = function() {  

  3.    PL.parameters.push({"name":"user-id", "value":"001");  

  4.    PL._init();  

  5.    PL.joinListen('/source/event');  

  6. };  

在HelloWorldPlushlet的pullEvent()方法調用:


  1. Session[] sessions = SessionManager.getInstance().getSessions();  

  2. String userId = sessions[0].getEvent().getField("user-id");  



這樣就實現了從頁面到pushlet的參數傳遞


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章