Package org.faceless.publisher.web
Class AbstractWebsocketSession
java.lang.Object
org.faceless.publisher.web.AbstractWebsocketSession
An abstraction of a Websocket sesssion.
Will be passed into a WebsocketController.
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Called when the Web Socket connection is closedabstract Authorization
Return theAuthorization
for this Web Socket connection.Return themessage_id
field from the last message received, or null if not set.abstract InetAddress
Return the remote address for the connection if known, or null if it cannot be retrievedabstract OutputStream
Return anOutputStream
to send binary content to the connectionabstract Writer
Return aWriter
to send text content to the connectionabstract ServiceEngine
Return theServiceEngine
referenced by this connectionabstract Object
getUserProperty
(String key) Retrieve a user-defined property previously set withputUserProperty()
Multiple threads may be processing multiple messages from a single logical session at once, so implementations must be synchronized and callers must be prepared for this.boolean
isBinary()
Return true if the last message received was in CBOR rather than JSONabstract boolean
isOpen()
Return true if the connection is currently openabstract void
Log a messagevoid
open()
Called when the Web Socket connection is openedabstract void
putUserProperty
(String key, Object property) Store a user-defined property against this Web Socket connection.void
receive
(InputStream in) Receive a binary message from theInputStream
, parse it as CBOR and call theWebsocketController.receive()
method with the result.void
Receive a text message from theReader
, parse it as JSON and call theWebsocketController.receive()
method with the result.Json
A convenience method to create a "message" type reply.Json
A convenience method to create a "message" type replyvoid
send
(Json json) Send a message as text or binary (depending in the value ifisBinary()
) to the object return fromgetSendStream()
orgetSendWriter()
as appropriate.void
Set theAuthorization
for this Web Socket connection.void
systemError
(Throwable err) Called to log a "system-error"
-
Field Details
-
JSON
The Media Type for JSON -
CBOR
The Media Type for CBOR
-
-
Constructor Details
-
AbstractWebsocketSession
public AbstractWebsocketSession()
-
-
Method Details
-
getRemoteAddress
Return the remote address for the connection if known, or null if it cannot be retrieved- Returns:
- the remote address
-
isOpen
public abstract boolean isOpen()Return true if the connection is currently open- Returns:
- the connection open state
-
isBinary
public boolean isBinary()Return true if the last message received was in CBOR rather than JSON- Returns:
- the binary flag
-
getMessageId
Return themessage_id
field from the last message received, or null if not set.- Returns:
- the
message_id
-
getSendWriter
Return aWriter
to send text content to the connection- Returns:
- a writer
- Throws:
IOException
- if the writer cannot be created
-
getSendStream
Return anOutputStream
to send binary content to the connection- Returns:
- a stream
- Throws:
IOException
- if the stream cannot be created
-
getServiceEngine
Return theServiceEngine
referenced by this connection- Returns:
- the ServiceEngine
-
putUserProperty
Store a user-defined property against this Web Socket connection. Multiple threads may be processing multiple messages from a single logical session at once, so implementations must be synchronized and callers must be prepared for this.- Parameters:
key
- the keyproperty
- the value to store or, ifnull
the key will be deleted
-
getUserProperty
Retrieve a user-defined property previously set withputUserProperty()
Multiple threads may be processing multiple messages from a single logical session at once, so implementations must be synchronized and callers must be prepared for this.- Parameters:
key
- the key- Returns:
- the value or null if not set
-
log
Log a message- Parameters:
s
- the message - may be nulle
- the Throwable - may be null
-
getAuthorization
Return theAuthorization
for this Web Socket connection. The returned value will never be null - at worst it will be a default Authorization- Returns:
- the Authorization
-
setAuthorization
Set theAuthorization
for this Web Socket connection.- Parameters:
auth
- the Authoization
-
receive
Receive a text message from theReader
, parse it as JSON and call theWebsocketController.receive()
method with the result.- Parameters:
reader
- the Reader to read from
-
receive
Receive a binary message from theInputStream
, parse it as CBOR and call theWebsocketController.receive()
method with the result.- Parameters:
in
- the InputStream to read from
-
send
public void send(Json json) Send a message as text or binary (depending in the value ifisBinary()
) to the object return fromgetSendStream()
orgetSendWriter()
as appropriate. If not already set, thereply_to
field will be set to the value fromgetMessageId()
- Parameters:
json
- the data to send.
-
open
public void open()Called when the Web Socket connection is opened -
close
Called when the Web Socket connection is closed- Parameters:
code
- the code for closure if one is known, or 0 otherwisereason
- the reason for closure if one is known, or null otherwise
-
systemError
Called to log a "system-error"- Parameters:
err
- the error
-
reply
A convenience method to create a "message" type reply- Parameters:
type
- the type of message to create, or null to use "message" or "error" depending on the presence of the throwableok
- the ok valuemsg
- the message, or null to use default texte
- if the message is an error message, the exception that caused it- Returns:
- a Json representation of this data
-
reply
A convenience method to create a "message" type reply. Callsreply(type, ok, msg, null)
- Parameters:
type
- the type of message to create, or null to use "message"ok
- the ok valuemsg
- the message, or null to use default text- Returns:
- a Json representation of this data
-