Skip to content

Advanced Server Side Processing

flowersinthesand edited this page Jul 1, 2012 · 1 revision

The following topics are not essential for two-way communication, but provide additional functions which can make the application more solid.

Heartbeat

The heartbeat event is used for connection management. If the request parameter heartbeat is in number format, the socket object in the client side continuously sends this event each specified time so the server should proceed according to the following instructions:

  • Prevent a connection from timing out if the value is number.
  • Set a heartbeat timer that closes the connection after the heartbeat interval.
  • When the client sends a heartbeat event, reset the timer and send back the event. If the server can't respond in 5000ms, the socket object in the client side will regard that the server dies and fire the close event.

Reply

This function uses the reply event. When the server deals with client-sent event if the value of the reply property of event object is true, the server should send a reply event to inform the client of acknowledgement of receipt. The server can respond lazily and can attach a result of handling event. The reply event's data should contain a id property whose value is client-sent event's id and can contain a data property whose value is a result of handling client-sent event. Then, the corresponding callback in the client-side will be executed with the result data.

Requesting the reply

The server, in common with the socket object in the client-side, can request a reply to the socket object.

  • Increment an event id, every time the server sends an event.
  • Set a reply property of an event object to true if the callback is not null.
  • Client sent reply event is the same with what the server creates. When the client sends a reply event, execute the corresponding callback with proper value.

Supporting credentials in the cross-origin connection

For the server to access user credentials such as cookies in the cross-origin connection, the server should set the Access-Control-Allow-Origin response header to the value of the Origin request header and the Access-Control-Allow-Credentials response header to true when handling GET and POST request. In this case, the * cannot be used as the value of the Access-Control-Allow-Origin header.

@WebServlet(urlPatterns = "/chat", asyncSupported = true)
public class ChatServlet extends HttpServlet {

    // Handles a GET request
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Credentials", "true");
        // ...
    }
    
    // Handles a POST request
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Credentials", "true");
        // ...
    }

    // ...

}

This is possible only if the credentials option is true and the browser supports the Cross-Origin Resource Sharing specificiation.

Clone this wiki locally