A kotlin multiplatform library to manage sockets with support for both iOS & Android
TitanSocket handles all the websocket connections, ping & pong between the client & server, and the event notifications for when data is received or broadcasted, or any connectivity status changes happen.
To get started, import the library into your project:
implementation("io.github.thearchitect123:titansocket:0.2.7")
To use TitanSocket, generate an instance of your socket, pass the Url + any Post Connection Logic, and subscribe to the states you wish to connect to:
val socketConnection = TitanSocket("wss://mysupersecret/websocket") {
subscribeOn(TitanSocketEvents.CONNECTION_OPENED) {}
subscribeOn(TitanSocketEvents.DISCONNECTION) {}
subscribeOn(TitanSocketEvents.FAILURE) {}
subscribeOn(TitanSocketEvents.MESSAGE_BINARY_RECEIVED) {}
subscribeOn(TitanSocketEvents.MESSAGE_RECEIVED) {}
subscribeOn(TitanSocketEvents.MESSAGE_SENDING) {}
})
Connect your telemetry Endpoints into TitanSocket to listen to Ping & Pong events.
val socketConnection = TitanSocket("wss://mysupersecret/websocket") {
subscribeOn(TitanSocketEvents.CONNECTION_OPENED) {}
subscribeOn(TitanSocketEvents.DISCONNECTION) {}
subscribeOn(TitanSocketEvents.FAILURE) {}
subscribeOn(TitanSocketEvents.MESSAGE_BINARY_RECEIVED) {}
subscribeOn(TitanSocketEvents.MESSAGE_RECEIVED) {}
subscribeOn(TitanSocketEvents.MESSAGE_SENDING) {}
}) {
onReceiveResponseWebSocket() {
println("RESPONSE $it")
}
onSendRequestWebSocket() {
println("REQUEST $it")
}
}
After your Socket is configured, startup the connection.
socketConnection.connectSocket()
Make sure to close your web socket connection after you are done with it to avoid battery issues.
socketConnection.disconnectSocket()
This software is licensed under the MIT license. See LICENSE for full disclosure.