scalaz-stream tcp`echo`应用程序不起作用

时间:2015-02-07 06:14:09

标签: scala scalaz scalaz-stream

我写了一个sendreceive '\0'终止字符串

的回应应用

https://gist.github.com/jilen/10a664cd588af10b7d09

object Foo {

  implicit val S = scalaz.concurrent.Strategy.DefaultStrategy
  implicit val AG = tcp.DefaultAsynchronousChannelGroup
  ...

  def runServer() {
    def writeStr(str: String) = tcp.write(ByteVector(str.getBytes))
    val echoServer = (readStr |> serverLog("[Server] Receiving")).flatMap(writeStr)
    val server = tcp.server(addr, concurrentRequests = 1)(echoServer.repeat)
    server.flatMap(_.drain).run.run
  }

  def runClient() {
    val topic = async.topic[String]()
    val echoClient = (topic.subscribe |> clientLog("[Client] Inputing")).map { str =>
      tcp.write(ByteVector(str.getBytes) ++ Delimiter) ++ (readStr |> clientLog("[Client] Receiving"))
    }
    val client = tcp.connect(addr)(tcp.lift(echoClient))
    client.run.runAsync(println)
    io.stdInLines.to(topic.publish).run.run
  }
}

我在不同的终端上运行Foo.runServer()Foo.runClient()

从客户端控制台输入数字 1 2 3 ... ,但客户端没有收到回复。

enter image description here enter image description here

我的echo应用程序有什么问题?

1 个答案:

答案 0 :(得分:0)

最后我将客户端重写为

val echoClient = tcp.subscribe.map(str => ByteVector(str.getBytes) ++ Delimiter)

val client = tcp.connect(addr)(tcp.writes(echoClient))

它有效