org.atmosphere.websocket
Class WebSocketHandler

java.lang.Object
  extended by org.atmosphere.websocket.WebSocketHandler
All Implemented Interfaces:
AsyncProtocol, WebSocketProtocol

public abstract class WebSocketHandler
extends Object
implements WebSocketProtocol

A very simple WebSocketProtocol adapter class that implements all methods and expose a WebSocket API close to the JavaScript Counterpart.

Author:
Jeanfrancois Arcand

Constructor Summary
WebSocketHandler()
           
 
Method Summary
 void configure(AtmosphereConfig config)
          Allow an implementation to query the AtmosphereConfig of init-param, etc.
 byte[] handleResponse(AtmosphereResponse res, byte[] message, int offset, int length)
          Give a chance to a AsyncProtocol to modify the final response using a fake AtmosphereResponse that was dispatched to a ServletContainer and it's framework or application running it.
 String handleResponse(AtmosphereResponse res, String message)
          Give a chance to a AsyncProtocol to modify the final response using a fake AtmosphereResponse that was dispatched to a ServletContainer and it's framework or application running it.
 boolean inspectResponse()
          Return true if this implementation will manipulate/change the WebSocket message;
 void onByteMessage(WebSocket webSocket, byte[] data, int offset, int length)
          Invoked when a byte message is received.
 void onClose(WebSocket webSocket)
          Invoked when a WebSocket is closed
 void onError(WebSocket webSocket, WebSocketProcessor.WebSocketException t)
          Invoked when an error occurs.
 List<AtmosphereRequest> onMessage(WebSocket webSocket, byte[] data, int offset, int length)
          Parse the WebSocket message, and delegate the processing to the AtmosphereFramework.asyncSupport or to any existing technology.
 List<AtmosphereRequest> onMessage(WebSocket webSocket, String data)
          Parse the WebSocket message, and delegate the processing to the AtmosphereFramework.asyncSupport or to any existing technology.
 void onOpen(WebSocket webSocket)
          Invoked when a WebSocket is opened
 void onTextMessage(WebSocket webSocket, String data)
          Invoked when a String message is received
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WebSocketHandler

public WebSocketHandler()
Method Detail

onByteMessage

public void onByteMessage(WebSocket webSocket,
                          byte[] data,
                          int offset,
                          int length)
Invoked when a byte message is received.

Parameters:
webSocket - a WebSocket
data -
offset -
length -

onTextMessage

public void onTextMessage(WebSocket webSocket,
                          String data)
Invoked when a String message is received

Parameters:
webSocket - a WebSocket
data -

configure

public final void configure(AtmosphereConfig config)
Allow an implementation to query the AtmosphereConfig of init-param, etc.

Specified by:
configure in interface WebSocketProtocol
Parameters:
config - AtmosphereConfig

onOpen

public void onOpen(WebSocket webSocket)
Invoked when a WebSocket is opened

Specified by:
onOpen in interface WebSocketProtocol
Parameters:
webSocket - WebSocket

onClose

public void onClose(WebSocket webSocket)
Invoked when a WebSocket is closed

Specified by:
onClose in interface WebSocketProtocol
Parameters:
webSocket - WebSocket

onError

public void onError(WebSocket webSocket,
                    WebSocketProcessor.WebSocketException t)
Invoked when an error occurs.

Specified by:
onError in interface WebSocketProtocol
Parameters:
webSocket - WebSocket
t - a WebSocketProcessor.WebSocketException

onMessage

public final List<AtmosphereRequest> onMessage(WebSocket webSocket,
                                               String data)
Parse the WebSocket message, and delegate the processing to the AtmosphereFramework.asyncSupport or to any existing technology. Invoking AtmosphereFramework.asyncSupport will delegate the request processing to the AtmosphereHandler implementation. Returning null means this implementation will handle itself the processing/dispatching of the WebSocket's request;
As an example, this is how Websocket messages are delegated to the Jersey runtime.

Specified by:
onMessage in interface WebSocketProtocol
Parameters:
webSocket - The WebSocket connection
data - The Websocket message

onMessage

public final List<AtmosphereRequest> onMessage(WebSocket webSocket,
                                               byte[] data,
                                               int offset,
                                               int length)
Parse the WebSocket message, and delegate the processing to the AtmosphereFramework.asyncSupport or to any existing technology. Invoking AtmosphereFramework.asyncSupport will delegate the request processing to the AtmosphereHandler implementation. Returning null means this implementation will handle itself the processing/dispatching of the WebSocket's request;
As an example, this is how Websocket messages are delegated to the Jersey runtime.

Specified by:
onMessage in interface WebSocketProtocol
Parameters:
webSocket - The WebSocket connection
offset - offset message index
length - length of the message.

inspectResponse

public final boolean inspectResponse()
Return true if this implementation will manipulate/change the WebSocket message;

Specified by:
inspectResponse in interface AsyncProtocol
Returns:
true if this implementation will manipulate/change the WebSocket message;

handleResponse

public final String handleResponse(AtmosphereResponse res,
                                   String message)
Give a chance to a AsyncProtocol to modify the final response using a fake AtmosphereResponse that was dispatched to a ServletContainer and it's framework or application running it. This method is only invoked when AtmosphereResponse is about to write some data.

Specified by:
handleResponse in interface AsyncProtocol
Parameters:
res - AtmosphereResponse
message - the String message;
Returns:
a new response String

handleResponse

public final byte[] handleResponse(AtmosphereResponse res,
                                   byte[] message,
                                   int offset,
                                   int length)
Give a chance to a AsyncProtocol to modify the final response using a fake AtmosphereResponse that was dispatched to a ServletContainer and it's framework or application running it. This method is only invoked when AtmosphereResponse is about to write some data.

Specified by:
handleResponse in interface AsyncProtocol
Parameters:
res - AtmosphereResponse
message - the WebSocket message;
offset - offset of the message
length - the length of the message
Returns:
a new byte[] message.


Copyright © 2012. All Rights Reserved.