Last active
April 14, 2019 10:46
-
-
Save meoyawn/037f25f9a12a263216c42edf66c97409 to your computer and use it in GitHub Desktop.
CLI instant messenger in 60 lines with ZMQ
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
package chat | |
import org.zeromq.ZMQ | |
import kotlin.concurrent.thread | |
fun main(args: Array<String>): Unit = | |
ZMQ.context(1).use { ctx -> | |
thread { | |
ctx.socket(ZMQ.SUB).use { stream -> | |
stream.connect(MESSAGE_STREAM) | |
stream.subscribe(ByteArray(0)) | |
while (true) { | |
val msg = stream.recvStr() | |
println("client got $msg") | |
} | |
} | |
} | |
ctx.socket(ZMQ.PUSH).use { push -> | |
push.connect(POST_MESSAGE) | |
while (true) { | |
val msg = readLine() | |
println("will send $msg") | |
push.send(msg) | |
} | |
} | |
} |
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
package chat | |
val POST_MESSAGE = "tcp://localhost:6573" | |
val MESSAGE_STREAM = "tcp://localhost:6574" |
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
package chat | |
import org.zeromq.ZMQ | |
fun main(args: Array<String>): Unit = | |
ZMQ.context(1).use { ctx -> | |
ctx.socket(ZMQ.PULL).use { post -> | |
post.bind(POST_MESSAGE) | |
ctx.socket(ZMQ.PUB).use { stream -> | |
stream.bind(MESSAGE_STREAM) | |
while (true) { | |
val msg = post.recvStr() | |
println("server got $msg") | |
stream.send(msg) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment