-
public interface StreamWebSocketListenerListener interface for receiving WebSocket events from the Stream API.
This interface provides callback hooks for the lifecycle of a WebSocket connection including creation, message delivery, failures, and closure events. It extends StreamSubscription so that listeners can be registered/unregistered consistently with other subscription-based APIs in Stream Core.
Implement this interface to handle WebSocket events in your client:
val listener = object : StreamWebSocketListener { override fun onOpen(response: Response) { println("Socket opened: ${response.message}") } override fun onMessage(text: String) { println("Received message: $text") } override fun onFailure(t: Throwable, response: Response?) { println("Socket failure: ${t.message}") } }
-
-
Method Summary
Modifier and Type Method Description UnitonOpen(Response response)Called when the socket connection is established. UnitonMessage(ByteString bytes)Called when a new binary message is received from the server. UnitonMessage(String text)Called when a new text message is received from the server. UnitonFailure(Throwable t, Response response)Called when an error occurs on the socket. UnitonClosed(Integer code, String reason)Called when the socket connection has been closed. UnitonClosing(Integer code, String reason)Called when the socket is about to close. -
-
Method Detail
-
onOpen
Unit onOpen(Response response)
Called when the socket connection is established.
- Parameters:
response- The handshake response returned by the server.
-
onMessage
Unit onMessage(ByteString bytes)
Called when a new binary message is received from the server.
- Parameters:
bytes- The raw binary payload received.
-
onMessage
Unit onMessage(String text)
Called when a new text message is received from the server.
- Parameters:
text- The UTF-8 encoded text payload received.
-
onFailure
Unit onFailure(Throwable t, Response response)
Called when an error occurs on the socket.
- Parameters:
t- The throwable cause of the failure.response- The optional server response associated with the error.
-
onClosed
Unit onClosed(Integer code, String reason)
Called when the socket connection has been closed.
- Parameters:
code- The closure status code as defined by RFC 6455.reason- The reason message provided by the peer, if any.
-
-
-
-