Class WebSocket
java.lang.Object
com.codename1.io.WebSocket
Client-side WebSocket. Connections are created via build(java.lang.String) and configured
with fluent handler setters before being started with connect():
WebSocket ws = WebSocket.build("wss://example.com/chat")
.onConnect(w -> w.send("hello"))
.onTextMessage((w, m) -> Log.p("recv " + m))
.onClose((w, c, r) -> Log.p("closed " + c + " / " + r))
.onError((w, e) -> Log.e(e))
.connect();
Each handler receives the WebSocket as its first argument so it can
send, query state, or close without capturing an external reference.
Handlers fire on a background thread. Use
Display.getInstance().callSerially(...) inside a handler if you need
to touch UI from it.
Use isSupported() to check at runtime whether the current port supports
WebSocket -- older ports return false and build(java.lang.String) will throw on them.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceHandler for an incoming binary frame.static interfaceHandler for the close event.static interfaceHandler for the connection-established event.static interfaceHandler for transport- or protocol-level errors.static interfaceHandler for an incoming text frame. -
Method Summary
Modifier and TypeMethodDescriptionstatic WebSocketCreate an unconnected WebSocket bound tourl.voidclose()Close the connection.connect()Start the handshake using the platform default connect timeout.connect(int connectTimeoutMs) Start the handshake with an explicit connect timeout in milliseconds.getUrl()static booleanWhether the current port supports WebSocket.onBinaryMessage(WebSocket.BinaryHandler handler) Register a handler for incoming binary frames.onClose(WebSocket.CloseHandler handler) Register a handler for the close event.onConnect(WebSocket.ConnectHandler handler) Register a handler for the connection-established event.onError(WebSocket.ErrorHandler handler) Register a handler for transport errors.onTextMessage(WebSocket.TextHandler handler) Register a handler for incoming text frames.voidsend(byte[] binary) Send a binary frame.voidSend a text frame.
-
Method Details
-
isSupported
public static boolean isSupported()Whether the current port supports WebSocket. -
build
Create an unconnected WebSocket bound tourl. The URL must use thews://orwss://scheme. Callconnect()to start the handshake.- Throws:
RuntimeException- if the current port does not support WebSocket.
-
onConnect
Register a handler for the connection-established event. Returnsthisfor chaining. -
onTextMessage
Register a handler for incoming text frames. Returnsthisfor chaining. -
onBinaryMessage
Register a handler for incoming binary frames. Returnsthisfor chaining. -
onClose
Register a handler for the close event. Returnsthisfor chaining. -
onError
Register a handler for transport errors. Returnsthisfor chaining. -
connect
Start the handshake using the platform default connect timeout. Returnsthisfor chaining; success is signalled asynchronously via the registeredWebSocket.ConnectHandler. -
connect
Start the handshake with an explicit connect timeout in milliseconds.0means "use platform default". -
close
public void close()Close the connection. Idempotent. -
send
Send a text frame. ThrowsIllegalStateExceptionif the connection is notWebSocketState.OPEN. -
send
public void send(byte[] binary) Send a binary frame. ThrowsIllegalStateExceptionif the connection is notWebSocketState.OPEN. -
getReadyState
-
getUrl
-