ktor-init-tools icon indicating copy to clipboard operation
ktor-init-tools copied to clipboard

Raw sockets sample issue

Open asimaranov opened this issue 5 years ago • 0 comments

The ktor plugin generates the follow code as a raw sockets sample. It doesn't seem to work. The blocking operation happens without Dispatchers.IO context. The launch block is never started

object Client {
        @JvmStatic
        fun main(args: Array<String>) {
            runBlocking {
                val socket = aSocket(selectorManager).tcp().connect("127.0.0.1", port = DefaultPort)
                val read = socket.openReadChannel()
                val write = socket.openWriteChannel(autoFlush = true)

                launch {
                    while (true) {
                        val line = read.readUTF8Line()
                        println("server: $line")
                    }
                }

                // No Dispatchers.IO context !!!!

                for (line in System.`in`.lines()) {
                    println("client: $line")
                    write.writeStringUtf8("$line\n")

                }
            }
        }

        private fun InputStream.lines() = Scanner(this).lines()

        private fun Scanner.lines() = sequence {
            while (hasNext()) {
                yield(readLine())
            }
        }
    }

asimaranov avatar Dec 08 '19 13:12 asimaranov