Created
February 11, 2026 19:59
-
-
Save LethalMaus/21350ef2e4e6cb4039020de49517580d to your computer and use it in GitHub Desktop.
ZeroMq.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun connect() { | |
| if (isRunning) return | |
| CoroutineScope(Dispatchers.IO).launch { | |
| try { | |
| context = ZContext() | |
| subPort?.let { | |
| subSocket = context?.createSocket(SocketType.SUB) | |
| subSocket?.subscribe("".toByteArray()) | |
| subSocket?.connect("tcp://127.0.0.1:$subPort") | |
| Log.d(TAG, "Subbing to $subPort") | |
| startReceiving() | |
| } | |
| pubPort?.let { | |
| pubSocket = context?.createSocket(SocketType.PUB) | |
| pubSocket?.connect("tcp://127.0.0.1:$pubPort") | |
| Log.d(TAG, "Pubbing to $pubPort") | |
| } | |
| isRunning = subSocket != null || pubSocket != null | |
| onConnectionChanged?.let { | |
| CoroutineScope(Dispatchers.Main).launch { it.invoke(true) } | |
| } | |
| } catch (e: Exception) { | |
| Log.e(TAG, "Error connecting to server: ${e.message}", e) | |
| onConnectionChanged?.let { | |
| CoroutineScope(Dispatchers.Main).launch { it.invoke(false) } | |
| } | |
| close() | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment