Caucho maker of Resin Server | Application Server (Java EE Certified) and Web Server


 

Resin Documentation

home company docs 
app server 
 Resin Server | Application Server (Java EE Certified) and Web Server
 

websocket


WebSocket

Resin's WebSocket API follows the Servlet API's stream model, using InputStream/OutputStream for binary messages and a Reader/PrintWriter for text messages. HTTP browsers will use text messages, while custom clients like phone/pad applications may use binary messages for efficiency.

The servlet resin-web.xml configuration is the same as for a normal servlet. On the initial request, the servlet will dispatch to a websocket listener.

WebSocketServletRequest

The servlet is responsible for detecting websocket requests and attaching a listener to that connection.

package example;

import com.caucho.websocket.WebSocketListener;
import com.caucho.websocket.WebSocketServletRequest;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;

public class MyServlet extends HttpServlet {

  public void service(HttpServletRequest req, HttpServletResponse res)
    throws IOException, ServletException
  {
    String protocol = req.getHeader("Sec-WebSocket-Protocol");

    WebSocketListener listener;

    if (! "my-protocol".equals(protocol)) {
      res.sendError(404);
      return;
    }

    listener = new MyListener();
    res.setHeader("Sec-WebSocket-Protocol", "my-protocol");
    
    WebSocketServletRequest wsReq = (WebSocketServletRequest) req;

    wsReq.startWebSocket(listener);
  }
}

WebSocketListener

The listener is responsible for the communcation and stateful data.

package example;

import com.caucho.websocket.WebSocketListener

import java.io.*;

import java.util.logging.Logger;

public class MyListener implements WebSocketListener
{
  private static final Logger log
    = Logger.getLogger(MyListener.class.getName());

  public void onStart(WebSocketContext context)
    throws IOException
  {
    log.info("onStart");
  }

  public void onReadBinary(WebSocketContext context, InputStream is)
    throws IOException
  {
    StringBuilder sb = new StringBuilder();
    int ch;
    
    while ((ch = is.read()) >= 0) {
      sb.append((char) ch);
    }
    
    log.info("onReadBinary: " + sb);
  }

  public void onReadText(WebSocketContext context, Reader is)
    throws IOException
  {
    StringBuilder sb = new StringBuilder();
    int ch;
    
    while ((ch = is.read()) >= 0) {
      sb.append((char) ch);
    }
    
    log.info("onReadText: " + sb);
  }

  public void onClose(WebSocketContext context)
    throws IOException
  {
    log.info("onClose");
  }

  public void onDisconnect(WebSocketContext context)
    throws IOException
  {
    log.info("onDisconnect");
  }

  public void onTimeout(WebSocketContext context)
    throws IOException
  {
    log.info("onTimeout");
  }
}


See com.caucho.websocket.WebSocketListener.


Copyright © 1998-2015 Caucho Technology, Inc. All rights reserved. Resin ® is a registered trademark. Quercustm, and Hessiantm are trademarks of Caucho Technology.

Cloud-optimized Resin Server is a Java EE certified Java Application Server, and Web Server, and Distributed Cache Server (Memcached).
Leading companies worldwide with demand for reliability and high performance web applications including SalesForce.com, CNET, DZone and many more are powered by Resin.

home company docs 
app server