pushlet-UserUnicast Manual


pushlet-UserUnicast is based on a modified extension JAVA comet pushlet after implementation framework. Major expansion of the unicast real demand push function.

The method unicast pushlet can push data to the specified session (SessionID), the data is pushed to the specified user, since the SessionID can be randomly generated, can not be associated with a user, it is not according to the service field (such as the user ID, user name) and so pushed to the specified user.

Core implementation features:

Explanation:

Note:

pushlet-userunicast API:

Client:

Server:

Multi-user connection configuration parameters:

When multiple clients to connect simultaneously use the same user ID PL.userId can be more connected to the push type parameters byunicast.type user, unicastUserId (Event, userId) method uses multiple connections that value as the user push the way.

In pushlet.properties can configureunicast.type parameters, optional value:

pushlet-userunicast Users push the use of steps and examples:

  1. Jar package introduced for the project under lib:
    pushlet-userunicast.jar
    log4j.jar

  2. Join the configuration file resource under the src orWEB-INF in:
    log4j.properties
    pushlet.properties
    sources.properties

  3. Need to subscribe to an event added to the Web project client under the client JS file:
    ajax-pushlet-client.js

  4. In web.xml core Servlet controller configurationpushlet

    <servlet>
         <servlet-name>pushlet</servlet-name>
         <servlet-class>
             nl.justobjects.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>
  5. Clients subscribe to receive messages index.jsp
    Use PL.userId attribute specifies the string that identifies the client, server-side push data in accordance with this identity to the client

     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     <html>
     <head>
         <title>Users receive a message page</title>
    
         <script type="text/javascript" src="js/ajax-pushlet-client.js"></script>
         <script type="text/javascript">
             PL._init();
             PL.userId = "JAY";
             PL.joinListen('/push/hello');
             function onData(event) {
                 //console.info(event.get("msg"));
                 document.getElementById("msg").innerHTML = document
                         .getElementById("msg").innerHTML
                         + event.get("msg") + "<br/>";
             }
         </script>
     </head>
    
     <body>
         <div id="msg"></div>
         <br />
         <br />
         <br />
         <a href="publish.jsp" target="_blank">Push back page news</a>
     </body>
     </html>
  6. Servlet server-side push test data
    Use Dispatcher.getInstance () unicastUserId (Event, userId user ID); push data to the specified client user ID, userId user ID corresponding PL.userId.

     package servlet;
    
     import java.io.IOException;
    
     import javax.servlet.ServletException;
     import javax.servlet.http.HttpServlet;
     import javax.servlet.http.HttpServletRequest;
     import javax.servlet.http.HttpServletResponse;
    
     import nl.justobjects.pushlet.core.Dispatcher;
     import nl.justobjects.pushlet.core.Event;
    
     public class MsgPushServlet extends HttpServlet {
    
         @Override
         protected void service(HttpServletRequest req, HttpServletResponse resp)
                 throws ServletException, IOException {
             req.setCharacterEncoding("utf-8");
             // Push message
             String msg=req.getParameter("msg");
     //        String userId="JAY";
             // Push users
             String userId=req.getParameter("userId");
             // Push-user multi-way connection
             String type=req.getParameter("type");
    
             // Event objects and data
             Event event = Event.createDataEvent("/push/hello");
             event.setField("msg", msg); //Chinese do not need to be converted to ISO-8859-1
    
             // According unicast.type push parameter values ​​to the specified user pushlet.properties
     //        Dispatcher.getInstance().unicastUserId(event,userId);  
    
              // Pushed to the specified user from the specified type type
             Dispatcher.getInstance().unicastUserId(event, userId, type); 
         }
     }

    The servlet web.xml configuration

    <servlet>
         <servlet-name>MsgPushServlet</servlet-name>
         <servlet-class>servlet.MsgPushServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
         <servlet-name>MsgPushServlet</servlet-name>
         <url-pattern>/servlet/MsgPushServlet</url-pattern>
    </servlet-mapping>
  7. Call Servlet, publish pages publish.jsp push message

     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     <html>
     <head>
          <title>Published push message</title>
     </head>
    
     <body>
         <h1>Published need to push the message:</h1>
         <form action="servlet/MsgPushServlet" method="post" target="pushFrame">
             <p>
                 push userId: <input type="text" name="userId" value="JAY" />
             </p>
             <p>
                 push message: <input type="text" name="msg" >
             </p>
             <p>
                 push type:  
                 <input type="radio" name="type" id="all" value="all" checked="checked">
                 <label for="all">ALL(all connection)</label>
                  <input type="radio" name="type" id="first" value="first">
                  <label for="first">FIRST (first connection))</label>
                  <input type="radio" name="type" id="last" value="last">
                  <label for="last">LAST (last connection))</label>
             </p>
             <input type="submit" value="push">
         </form>
    
         <iframe name="pushFrame" style="display: none;"></iframe>
     </body>
     </html>

End

If you have more comments, suggestions or ideas, please contact me.

Contact, feedback, customization, training Email: inthinkcolor@gmail.com