org.atmosphere.websocket
Class WebSocket

java.lang.Object
  extended by org.atmosphere.cpr.AsyncIOWriterAdapter
      extended by org.atmosphere.cpr.AtmosphereInterceptorWriter
          extended by org.atmosphere.websocket.WebSocket
All Implemented Interfaces:
AsyncIOWriter
Direct Known Subclasses:
Grizzly2WebSocket, GrizzlyWebSocket, JBossWebSocket, Jetty8WebSocket, Jetty9WebSocket, JSR356WebSocket, TomcatWebSocket, WebLogicWebSocket

public abstract class WebSocket
extends AtmosphereInterceptorWriter

Represent a portable WebSocket implementation which can be used to write message.

Author:
Jeanfrancois Arcand

Field Summary
protected  ByteBuffer bb
           
protected  boolean binaryWrite
           
protected  CharBuffer cb
           
static String CLEAN_CLOSE
           
protected  long lastWrite
           
protected static org.slf4j.Logger logger
           
static String NOT_SUPPORTED
           
static String WEBSOCKET_ACCEPT_DONE
           
static String WEBSOCKET_INITIATED
           
static String WEBSOCKET_RESUME
           
static String WEBSOCKET_SUSPEND
           
 
Fields inherited from class org.atmosphere.cpr.AtmosphereInterceptorWriter
filters
 
Constructor Summary
WebSocket(AtmosphereConfig config)
           
 
Method Summary
 WebSocket binaryWrite(boolean binaryWrite)
          Switch to binary write, or go back to text write.
 WebSocket broadcast(Object o)
          Broadcast, using the AtmosphereResource.getBroadcaster() the object to all WebSocket associated with the Broadcaster.
abstract  void close()
          Close the underlying WebSocket
 void close(AtmosphereResponse r)
          Close the underlying connection.
 AtmosphereConfig config()
           
 WebSocket flush(AtmosphereResponse r)
          Flush the buffered content.
abstract  boolean isOpen()
          Is the underlying WebSocket open.
 long lastWriteTimeStampInMilliseconds()
          The last time, in milliseconds, a write operation occurred.
static void notSupported(AtmosphereRequest request, AtmosphereResponse response)
           
 WebSocket redirect(AtmosphereResponse r, String location)
          Redirect a WebSocket request to another location.
 AtmosphereResource resource()
          Return the an AtmosphereResource used by this WebSocket, or null if the WebSocket has been closed before the WebSocket message has been processed.
 WebSocket resource(AtmosphereResource r)
          Associate an AtmosphereResource to this WebSocket
protected  String retrieveUUID()
           
protected  byte[] transform(byte[] b, int offset, int length)
           
protected  WebSocketHandler webSocketHandler()
           
protected  WebSocket webSocketHandler(WebSocketHandler webSocketHandler)
           
 WebSocket write(AtmosphereResponse r, byte[] data)
          Write a WebSocket message.
 WebSocket write(AtmosphereResponse r, byte[] b, int offset, int length)
          Write a WebSocket message.
 WebSocket write(AtmosphereResponse r, String data)
          Write a WebSocket message.
abstract  WebSocket write(byte[] b, int offset, int length)
          Use the underlying container's websocket to write the byte.
abstract  WebSocket write(String s)
          Use the underlying container's websocket to write the String.
 WebSocket writeError(AtmosphereResponse r, int errorCode, String message)
          Write an error code.
 
Methods inherited from class org.atmosphere.cpr.AtmosphereInterceptorWriter
interceptor, invokeInterceptor, writeReady
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected static final org.slf4j.Logger logger

WEBSOCKET_INITIATED

public static final String WEBSOCKET_INITIATED

WEBSOCKET_SUSPEND

public static final String WEBSOCKET_SUSPEND

WEBSOCKET_RESUME

public static final String WEBSOCKET_RESUME

WEBSOCKET_ACCEPT_DONE

public static final String WEBSOCKET_ACCEPT_DONE

NOT_SUPPORTED

public static final String NOT_SUPPORTED
See Also:
Constant Field Values

CLEAN_CLOSE

public static final String CLEAN_CLOSE
See Also:
Constant Field Values

lastWrite

protected long lastWrite

binaryWrite

protected boolean binaryWrite

bb

protected ByteBuffer bb

cb

protected CharBuffer cb
Constructor Detail

WebSocket

public WebSocket(AtmosphereConfig config)
Method Detail

config

public AtmosphereConfig config()

webSocketHandler

protected WebSocket webSocketHandler(WebSocketHandler webSocketHandler)

binaryWrite

public WebSocket binaryWrite(boolean binaryWrite)
Switch to binary write, or go back to text write. Default is false.

Parameters:
binaryWrite - true to switch to binary write.
Returns:

webSocketHandler

protected WebSocketHandler webSocketHandler()

resource

public WebSocket resource(AtmosphereResource r)
Associate an AtmosphereResource to this WebSocket

Parameters:
r - an AtmosphereResource to this WebSocket
Returns:
this

resource

public AtmosphereResource resource()
Return the an AtmosphereResource used by this WebSocket, or null if the WebSocket has been closed before the WebSocket message has been processed.

Returns:
AtmosphereResource

lastWriteTimeStampInMilliseconds

public long lastWriteTimeStampInMilliseconds()
The last time, in milliseconds, a write operation occurred.

Returns:
this

transform

protected byte[] transform(byte[] b,
                           int offset,
                           int length)
                    throws IOException
Throws:
IOException

write

public WebSocket write(AtmosphereResponse r,
                       String data)
                throws IOException
Description copied from interface: AsyncIOWriter
Write a WebSocket message.

Specified by:
write in interface AsyncIOWriter
Overrides:
write in class AtmosphereInterceptorWriter
data - the WebSocket message
Throws:
IOException

write

public WebSocket write(AtmosphereResponse r,
                       byte[] data)
                throws IOException
Description copied from interface: AsyncIOWriter
Write a WebSocket message.

Specified by:
write in interface AsyncIOWriter
Overrides:
write in class AtmosphereInterceptorWriter
data - the WebSocket message
Throws:
IOException

write

public WebSocket write(AtmosphereResponse r,
                       byte[] b,
                       int offset,
                       int length)
                throws IOException
Description copied from interface: AsyncIOWriter
Write a WebSocket message.

Specified by:
write in interface AsyncIOWriter
Overrides:
write in class AtmosphereInterceptorWriter
b - the WebSocket message
offset - offset of the message
length - length of the message
Throws:
IOException

broadcast

public WebSocket broadcast(Object o)
Broadcast, using the AtmosphereResource.getBroadcaster() the object to all WebSocket associated with the Broadcaster. This method does the same as websocket.resource().getBroadcaster().broadcast(o).

Parameters:
o - An object to broadcast to all WebSockets.

writeError

public WebSocket writeError(AtmosphereResponse r,
                            int errorCode,
                            String message)
                     throws IOException
Description copied from interface: AsyncIOWriter
Write an error code.

Specified by:
writeError in interface AsyncIOWriter
Overrides:
writeError in class AtmosphereInterceptorWriter
errorCode - the error code
Throws:
IOException

redirect

public WebSocket redirect(AtmosphereResponse r,
                          String location)
                   throws IOException
Description copied from interface: AsyncIOWriter
Redirect a WebSocket request to another location.

Specified by:
redirect in interface AsyncIOWriter
Overrides:
redirect in class AtmosphereInterceptorWriter
Throws:
IOException

close

public void close(AtmosphereResponse r)
           throws IOException
Description copied from interface: AsyncIOWriter
Close the underlying connection.

Specified by:
close in interface AsyncIOWriter
Overrides:
close in class AtmosphereInterceptorWriter
Throws:
IOException

flush

public WebSocket flush(AtmosphereResponse r)
                throws IOException
Description copied from interface: AsyncIOWriter
Flush the buffered content.

Specified by:
flush in interface AsyncIOWriter
Overrides:
flush in class AtmosphereInterceptorWriter
Throws:
IOException

isOpen

public abstract boolean isOpen()
Is the underlying WebSocket open.

Returns:
true is opened

write

public abstract WebSocket write(String s)
                         throws IOException
Use the underlying container's websocket to write the String.

Parameters:
s - a websocket String message
Returns:
this
Throws:
IOException

write

public abstract WebSocket write(byte[] b,
                                int offset,
                                int length)
                         throws IOException
Use the underlying container's websocket to write the byte.

Parameters:
b - a websocket byte message
offset - start
length - end
Returns:
this
Throws:
IOException

close

public abstract void close()
Close the underlying WebSocket


retrieveUUID

protected String retrieveUUID()

notSupported

public static void notSupported(AtmosphereRequest request,
                                AtmosphereResponse response)
                         throws IOException
Throws:
IOException


Copyright © 2014. All Rights Reserved.