androidMain.com.yazantarifi.websocketmanager.manager.SocketClientManager.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of websockets Show documentation
Show all versions of websockets Show documentation
Socket IO Implementation for Android, IOS Apps - KMM
package com.yazantarifi.websocketmanager.manager
import com.yazantarifi.websocketmanager.configuration.SocketClientConfiguration
import com.yazantarifi.websocketmanager.configuration.SocketHooksCallback
import com.yazantarifi.websocketmanager.errors.SocketManagerException
import com.yazantarifi.websocketmanager.errors.childs.SocketInitException
import com.yazantarifi.websocketmanager.errors.childs.SocketNotConnectedException
import com.yazantarifi.websocketmanager.errors.childs.SocketValidationNotCalledException
import io.socket.client.Socket
import kotlin.jvm.Throws
class SocketClientManager : BaseClientManager() {
private var socketInstance: BaseSocketInstance? = null
private var configuration: SocketClientConfiguration? = null
private var isBuildSuccessfullyRegistered: Boolean = false
private var errorListenerCallback: ((SocketManagerException) -> Unit?)? = null
override fun initSocketManager(socketInstance: BaseSocketInstance): SocketClientManager {
this.socketInstance = socketInstance
return this
}
override fun registerSocketConnectionHooks(listener: SocketHooksCallback): SocketClientManager {
if (isBuildSuccessfullyRegistered) {
socketInstance?.registerSocketConnectionHooks(listener)
return this
}
if (errorListenerCallback != null) {
errorListenerCallback?.invoke(SocketValidationNotCalledException())
} else {
throw SocketValidationNotCalledException()
}
return this
}
override fun registerDefaultSocketInstanceCallbacks(): SocketClientManager {
if (isBuildSuccessfullyRegistered) {
socketInstance?.registerDefaultSocketInstanceCallbacks()
return this
}
if (errorListenerCallback != null) {
errorListenerCallback?.invoke(SocketValidationNotCalledException())
} else {
throw SocketValidationNotCalledException()
}
return this
}
override fun initInstanceConfiguration(configuration: SocketClientConfiguration): SocketClientManager {
this.configuration = configuration
return this
}
override fun registerErrorListener(errorListener: (SocketManagerException) -> Unit): SocketClientManager {
errorListenerCallback = errorListener
socketInstance?.registerErrorListener(errorListener)
return this
}
@Throws(SocketValidationNotCalledException::class)
override fun connectSocketInstance() {
if (isBuildSuccessfullyRegistered) {
socketInstance?.connectSocketInstance()
return
}
if (errorListenerCallback != null) {
errorListenerCallback?.invoke(SocketValidationNotCalledException())
} else {
throw SocketValidationNotCalledException()
}
}
override fun onSubscribeTopic(topicName: String, result: (Any) -> Unit) {
if (socketInstance?.isInstanceConnected() == false) {
errorListenerCallback?.invoke(SocketNotConnectedException())
return
}
socketInstance?.onSubscribeTopic(topicName, result)
}
override fun onSendEvent(eventName: String, payload: Any, result: (Any) -> Unit) {
if (socketInstance?.isInstanceConnected() == false) {
errorListenerCallback?.invoke(SocketNotConnectedException())
return
}
socketInstance?.onSendEvent(eventName, payload, result)
}
@Throws(SocketValidationNotCalledException::class)
override fun disconnectSocketInstance() {
if (isBuildSuccessfullyRegistered) {
socketInstance?.disconnectSocketInstance()
return
}
if (errorListenerCallback != null) {
errorListenerCallback?.invoke(SocketValidationNotCalledException())
} else {
throw SocketValidationNotCalledException()
}
}
@Throws(SocketValidationNotCalledException::class)
override fun isSocketConnected(): Boolean {
if (isBuildSuccessfullyRegistered) {
return socketInstance?.isInstanceConnected() ?: false
}
if (errorListenerCallback != null) {
errorListenerCallback?.invoke(SocketValidationNotCalledException())
return false
} else {
throw SocketValidationNotCalledException()
}
}
@Throws(SocketInitException::class)
override fun build(): SocketClientManager {
val isErrorListenerRegistered = errorListenerCallback != null
if (configuration == null) {
if (isErrorListenerRegistered) errorListenerCallback?.invoke(SocketInitException.getConfigurationInstance()) else throw SocketInitException.getConfigurationInstance()
}
if (socketInstance == null) {
if (isErrorListenerRegistered) errorListenerCallback?.invoke(SocketInitException.getSocketInstance()) else throw SocketInitException.getSocketInstance()
}
isBuildSuccessfullyRegistered = true
socketInstance?.buildSocketInstance()
return this
}
}