Package dev.gmitch215.bytebox.socket
Interface WebSocket
- All Superinterfaces:
org.teavm.jso.JSObject
public interface WebSocket
extends org.teavm.jso.JSObject
One end of a WebSocket.
The only connection a client can open to a Worker that is not a single request. A Worker accepts one by answering a request with status 101 and the other half of a pair; a Durable Object accepts one into its own set, which is what lets several clients reach the same object.
WebSocketPair pair = WebSocketPair.create();
state.acceptWebSocket(pair.server());
return Bytebox.upgrade(pair.client());
Messages arrive on the object's handler rather than through a listener, because a Worker has no ambient loop to run one in.
- Since:
- 1.0.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe application's own error, and the highest code an application may send.static final intThe endpoint is going away.static final intNormal closure: the purpose the connection was opened for is finished.static final intA message the endpoint cannot accept, which is the code to use for a protocol error. -
Method Summary
Modifier and TypeMethodDescriptionvoidAttaches state that survives hibernation.Returns whatattach(dev.gmitch215.bytebox.js.TSObject)stored, ornull.default voidclose()Closes the connection normally.voidCloses the connection.intReturns the ready state.default booleanisOpen()Returns whether the connection can still carry a message.default voidsend(byte[] message) Sends bytes.voidSends text.voidsend(org.teavm.jso.typedarrays.ArrayBuffer message) Sends bytes.default voidSends a value as JSON.Methods inherited from interface org.teavm.jso.JSObject
cast
-
Field Details
-
NORMAL
static final int NORMALNormal closure: the purpose the connection was opened for is finished.- See Also:
-
GOING_AWAY
static final int GOING_AWAYThe endpoint is going away.- See Also:
-
UNACCEPTABLE
static final int UNACCEPTABLEA message the endpoint cannot accept, which is the code to use for a protocol error.- See Also:
-
APPLICATION_ERROR
static final int APPLICATION_ERRORThe application's own error, and the highest code an application may send.- See Also:
-
-
Method Details
-
send
Sends text.- Parameters:
message- the text
-
send
void send(org.teavm.jso.typedarrays.ArrayBuffer message) Sends bytes.- Parameters:
message- the bytes
-
send
default void send(byte[] message) Sends bytes.- Parameters:
message- the bytes
-
sendJson
Sends a value as JSON.- Parameters:
message- the value
-
close
Closes the connection.- Parameters:
code- why, one of the constants here or an application code from 4000 to 4999reason- a description, at most 123 bytes once encoded
-
close
default void close()Closes the connection normally. -
attach
Attaches state that survives hibernation.A hibernating Durable Object drops its memory and runs its constructor again when a message arrives, so anything the handler needs about this connection has to live on the connection. At most 2 KB once serialised.
- Parameters:
value- the state
-
attachment
TSObject attachment()Returns whatattach(dev.gmitch215.bytebox.js.TSObject)stored, ornull.- Returns:
- what
attach(dev.gmitch215.bytebox.js.TSObject)stored, ornull
-
getReadyState
int getReadyState()Returns the ready state.0 connecting, 1 open, 2 closing, 3 closed.
- Returns:
- the ready state
-
isOpen
default boolean isOpen()Returns whether the connection can still carry a message.- Returns:
- whether the connection can still carry a message
-