Package org.atmosphere.websocket
Class WebSocket
- java.lang.Object
-
- org.atmosphere.cpr.AsyncIOWriterAdapter
-
- org.atmosphere.cpr.AtmosphereInterceptorWriter
-
- org.atmosphere.websocket.WebSocket
-
- All Implemented Interfaces:
AsyncIOWriter,KeepOpenStreamAware
- Direct Known Subclasses:
Grizzly2WebSocket,GrizzlyWebSocket,JBossWebSocket,Jetty8WebSocket,Jetty9WebSocket,JSR356WebSocket,TomcatWebSocket,WebLogicWebSocket
public abstract class WebSocket extends AtmosphereInterceptorWriter implements KeepOpenStreamAware
Represent a portable WebSocket implementation which can be used to write message.- Author:
- Jeanfrancois Arcand
-
-
Field Summary
Fields Modifier and Type Field Description protected java.nio.ByteBufferbbprotected booleanbinaryWriteprotected java.nio.CharBuffercbstatic java.lang.StringCLEAN_CLOSEprotected longlastWriteprotected static org.slf4j.Loggerloggerstatic java.lang.StringNOT_SUPPORTEDprotected java.lang.Stringuuidstatic java.lang.StringWEBSOCKET_ACCEPT_DONEstatic java.lang.StringWEBSOCKET_INITIATEDstatic java.lang.StringWEBSOCKET_RESUMEstatic java.lang.StringWEBSOCKET_SUSPEND-
Fields inherited from class org.atmosphere.cpr.AtmosphereInterceptorWriter
filters, reversedFilters
-
-
Constructor Summary
Constructors Constructor Description WebSocket(AtmosphereConfig config)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description java.lang.Objectattachment()Return the attachmentWebSocketattachment(java.lang.Object attachment)Attach an object.java.util.Map<java.lang.String,java.lang.Object>attributes()Return the attribute that was set during the websocket's open operation.WebSocketbinaryWrite(boolean binaryWrite)Switch to binary write, or go back to text write.WebSocketbroadcast(java.lang.Object o)Broadcast, using theAtmosphereResource.getBroadcaster()the object to allWebSocketassociated with theBroadcaster.abstract voidclose()Close the underlying WebSocketvoidclose(AtmosphereResponse r)Close the underlying connection.AtmosphereConfigconfig()WebSocketflush(AtmosphereResponse r)Flush the buffered content.abstract booleanisOpen()Is the underlying WebSocket open.longlastWriteTimeStampInMilliseconds()The last time, in milliseconds, a write operation occurred.static voidnotSupported(AtmosphereRequest request, AtmosphereResponse response)WebSocketredirect(AtmosphereResponse r, java.lang.String location)Redirect a WebSocket request to another location.AtmosphereResourceresource()Return the anAtmosphereResourceused by this WebSocket, or null if the WebSocket has been closed before the WebSocket message has been processed.WebSocketresource(AtmosphereResource r)Associate anAtmosphereResourceto this WebSocketWebSocketsendPing(byte[] payload)Send a WebSocket PingWebSocketsendPong(byte[] payload)Send a WebSocket PongWebSocketshiftAttributes()CopyAtmosphereRequestImpl.localAttributes()that where set when the websocket was opened.protected byte[]transform(byte[] b, int offset, int length)protected byte[]transform(AtmosphereResponse response, byte[] b, int offset, int length)java.lang.Stringuuid()WebSocketHandlerwebSocketHandler()protected WebSocketwebSocketHandler(WebSocketHandler webSocketHandler)WebSocketwrite(byte[] b)Use the underlying container's websocket to write the byte.abstract WebSocketwrite(byte[] b, int offset, int length)Use the underlying container's websocket to write the byte.abstract WebSocketwrite(java.lang.String s)Use the underlying container's websocket to write the String.WebSocketwrite(AtmosphereResponse r, byte[] data)Write a WebSocket message.WebSocketwrite(AtmosphereResponse r, byte[] b, int offset, int length)Write a WebSocket message.WebSocketwrite(AtmosphereResponse r, java.lang.String data)Write a WebSocket message.WebSocketwriteError(AtmosphereResponse r, int errorCode, java.lang.String message)Write an error code.-
Methods inherited from class org.atmosphere.cpr.AtmosphereInterceptorWriter
filters, interceptor, interceptor, invokeInterceptor, writeReady
-
-
-
-
Field Detail
-
logger
protected static final org.slf4j.Logger logger
-
WEBSOCKET_INITIATED
public static final java.lang.String WEBSOCKET_INITIATED
-
WEBSOCKET_SUSPEND
public static final java.lang.String WEBSOCKET_SUSPEND
-
WEBSOCKET_RESUME
public static final java.lang.String WEBSOCKET_RESUME
-
WEBSOCKET_ACCEPT_DONE
public static final java.lang.String WEBSOCKET_ACCEPT_DONE
-
NOT_SUPPORTED
public static final java.lang.String NOT_SUPPORTED
- See Also:
- Constant Field Values
-
CLEAN_CLOSE
public static final java.lang.String CLEAN_CLOSE
- See Also:
- Constant Field Values
-
lastWrite
protected long lastWrite
-
binaryWrite
protected boolean binaryWrite
-
bb
protected java.nio.ByteBuffer bb
-
cb
protected java.nio.CharBuffer cb
-
uuid
protected java.lang.String uuid
-
-
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
public WebSocketHandler webSocketHandler()
-
resource
public WebSocket resource(AtmosphereResource r)
Associate anAtmosphereResourceto this WebSocket- Parameters:
r- anAtmosphereResourceto this WebSocket- Returns:
- this
-
shiftAttributes
public WebSocket shiftAttributes()
CopyAtmosphereRequestImpl.localAttributes()that where set when the websocket was opened.- Returns:
- this.
-
attributes
public java.util.Map<java.lang.String,java.lang.Object> attributes()
Return the attribute that was set during the websocket's open operation.- Returns:
-
resource
public AtmosphereResource resource()
Return the anAtmosphereResourceused 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 java.io.IOException- Throws:
java.io.IOException
-
transform
protected byte[] transform(AtmosphereResponse response, byte[] b, int offset, int length) throws java.io.IOException
- Throws:
java.io.IOException
-
write
public WebSocket write(AtmosphereResponse r, java.lang.String data) throws java.io.IOException
Description copied from interface:AsyncIOWriterWrite a WebSocket message.- Specified by:
writein interfaceAsyncIOWriter- Overrides:
writein classAtmosphereInterceptorWriterdata- the WebSocket message- Throws:
java.io.IOException
-
write
public WebSocket write(AtmosphereResponse r, byte[] data) throws java.io.IOException
Description copied from interface:AsyncIOWriterWrite a WebSocket message.- Specified by:
writein interfaceAsyncIOWriter- Overrides:
writein classAtmosphereInterceptorWriterdata- the WebSocket message- Throws:
java.io.IOException
-
write
public WebSocket write(AtmosphereResponse r, byte[] b, int offset, int length) throws java.io.IOException
Description copied from interface:AsyncIOWriterWrite a WebSocket message.- Specified by:
writein interfaceAsyncIOWriter- Overrides:
writein classAtmosphereInterceptorWriterb- the WebSocket messageoffset- offset of the messagelength- length of the message- Throws:
java.io.IOException
-
broadcast
public WebSocket broadcast(java.lang.Object o)
Broadcast, using theAtmosphereResource.getBroadcaster()the object to allWebSocketassociated with theBroadcaster. 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, java.lang.String message) throws java.io.IOException
Description copied from interface:AsyncIOWriterWrite an error code.- Specified by:
writeErrorin interfaceAsyncIOWriter- Overrides:
writeErrorin classAtmosphereInterceptorWritererrorCode- the error code- Throws:
java.io.IOException
-
redirect
public WebSocket redirect(AtmosphereResponse r, java.lang.String location) throws java.io.IOException
Description copied from interface:AsyncIOWriterRedirect a WebSocket request to another location.- Specified by:
redirectin interfaceAsyncIOWriter- Overrides:
redirectin classAtmosphereInterceptorWriter- Throws:
java.io.IOException
-
close
public void close(AtmosphereResponse r) throws java.io.IOException
Description copied from interface:AsyncIOWriterClose the underlying connection.- Specified by:
closein interfaceAsyncIOWriter- Overrides:
closein classAtmosphereInterceptorWriter- Throws:
java.io.IOException
-
flush
public WebSocket flush(AtmosphereResponse r) throws java.io.IOException
Description copied from interface:AsyncIOWriterFlush the buffered content.- Specified by:
flushin interfaceAsyncIOWriter- Overrides:
flushin classAtmosphereInterceptorWriter- Throws:
java.io.IOException
-
isOpen
public abstract boolean isOpen()
Is the underlying WebSocket open.- Returns:
- true is opened
-
write
public abstract WebSocket write(java.lang.String s) throws java.io.IOException
Use the underlying container's websocket to write the String.- Parameters:
s- a websocket String message- Returns:
- this
- Throws:
java.io.IOException
-
write
public abstract WebSocket write(byte[] b, int offset, int length) throws java.io.IOException
Use the underlying container's websocket to write the byte.- Parameters:
b- a websocket byte messageoffset- startlength- end- Returns:
- this
- Throws:
java.io.IOException
-
write
public WebSocket write(byte[] b) throws java.io.IOException
Use the underlying container's websocket to write the byte.- Parameters:
b- a websocket byte message- Returns:
- this
- Throws:
java.io.IOException
-
close
public abstract void close()
Close the underlying WebSocket
-
uuid
public java.lang.String uuid()
-
notSupported
public static void notSupported(AtmosphereRequest request, AtmosphereResponse response) throws java.io.IOException
- Throws:
java.io.IOException
-
sendPing
public WebSocket sendPing(byte[] payload)
Send a WebSocket Ping- Parameters:
payload- the bytes to send- Returns:
- this
-
sendPong
public WebSocket sendPong(byte[] payload)
Send a WebSocket Pong- Parameters:
payload- the bytes to send- Returns:
- this
-
attachment
public WebSocket attachment(java.lang.Object attachment)
Attach an object. Be careful when attaching an object as it can cause memory leak
-
attachment
public java.lang.Object attachment()
Return the attachment
-
-