![]() | Resin Documentationapp server |
websocketResin'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. WebSocketServletRequestThe 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);
}
}
WebSocketListenerThe 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");
}
}
|