ktorio / ktor-init-tools

ktor project generator web page and IDEA plugin

Home Page:https://start.ktor.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Raw sockets sample issue

asimaranov opened this issue · 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())
            }
        }
    }