路由器挂在经销商 - 路由器设置中

时间:2016-08-09 02:37:41

标签: scala zeromq

鉴于以下尝试将1 DEALER连接到1 ROUTER

package net.async

import org.zeromq.ZMQ
import org.zeromq.ZMQ.Socket

import scala.annotation.tailrec

object Client {
  val Empty           = "".getBytes
  def message(x: Int) = s"HELLO_#$x".getBytes
  val Count   = 5
}

class Client(name: String) extends Runnable {

  import Client._
  import AsyncClientServer.Port

  override def run(): Unit = {
    val context = ZMQ.context(1)
    val dealer = context.socket(ZMQ.DEALER)
    dealer.setIdentity(name.getBytes)
    dealer.connect(s"tcp://localhost:$Port")
    runHelper(dealer, Count)
  }

  @tailrec
  private def runHelper(dealer: Socket, count: Int): Unit = {
    dealer.send(dealer.getIdentity, ZMQ.SNDMORE)
    dealer.send(Empty,              ZMQ.SNDMORE)
    dealer.send(message(count),     0)
    println(s"Dealer: ${dealer.getIdentity} received message: " + dealer.recv(0))
    runHelper(dealer, count - 1)
  }
}

object AsyncClientServer {

  val Port = 5555

  val context = ZMQ.context(1)
  val router = context.socket(ZMQ.ROUTER)

  def main(args: Array[String]): Unit = {
    router.bind(s"tcp://*:$Port")
    mainHelper()
    new Thread(new Client("Joe")).start()
  }

  private def mainHelper(): Unit = {
    println("Waiting to receive messages from Dealer.")
    val identity = router.recv(0)
    val empty    = router.recv(0)
    val message  = router.recv(0)

    println(s"Router received message, ${new String(message)} from sender: ${new String(identity)}.")
    mainHelper()
  }
}

我看到以下输出,挂在第二条消息上。

[info] Running net.async.AsyncClientServer
[info] Waiting to receive messages from Dealer.

为什么?

1 个答案:

答案 0 :(得分:1)

不确定是否是问题的原因但您不需要从经销商处发送身份信息框,zeromq会为您执行此操作。通过添加它,您实际发送了4部分消息。

IDENTITY
IDENTITY
EMPTY
CONTENT