回复不会传输回“客户端”参与者

发布于 2024-12-08 09:26:16 字数 1235 浏览 1 评论 0原文

使用远程演员时,我出现了意外的行为。我有一个服务器和一个“客户端”。客户端向服务器参与者发送消息,服务器回复。当我使用“?”时操作员一切都按预期进行。我从服务器得到答案。

服务器:

class HelloWorldActor extends Actor {
  def receive = {
    case msg => self reply (msg + " World")
  }
}

object Server extends App{
  Actor.remote.start("localhost",2552);
  Actor.remote.register("hello-service",Actor.actorOf[HelloWorldActor])
}

客户端:

object Client extends App {
  // client code
  val actor = remote.actorFor(
    "hello-service", "localhost", 2552)
  val result = (actor ? "Hello").as[String]
  println(result)
}

现在我更改了代码,以便客户端成为参与者并且只对答案做出反应。但是,回复不会发送回客户端。相反,在服务器上创建一个“ClientActor”实例并将回复发送到那里?

修改后的客户端:

class ClientActor extends Actor {
  def receive = {
    case "Ask" =>{
      val actor = remote.actorFor(
        "hello-service", "localhost", 2552)
      actor ! "Hello"
    }
    case response:String => println(response) // This executed on the server! That's not what I expect?
  }
}

object Client extends App {
  // client code
  val client = actorOf[ClientActor].start();
  client ! "Ask"
}

我缺少什么?这是 Akka 的预期行为吗?我怎样才能强迫它把答案发回给客户端呢?

感谢您的任何意见。

I've an unexpected behavior when using remote actors. I've a server and a 'client'. The client sends a message to the server actor and the server replies. When I use the '?' operator everything works as expected. I get the answer back from the server.

The server:

class HelloWorldActor extends Actor {
  def receive = {
    case msg => self reply (msg + " World")
  }
}

object Server extends App{
  Actor.remote.start("localhost",2552);
  Actor.remote.register("hello-service",Actor.actorOf[HelloWorldActor])
}

The client:

object Client extends App {
  // client code
  val actor = remote.actorFor(
    "hello-service", "localhost", 2552)
  val result = (actor ? "Hello").as[String]
  println(result)
}

Now I changed the code so that the client is an actor and just reacts to the answer. However not the reply isn't sent back to the client. Instead a 'ClientActor'-instance is created one the server and the reply is sent there?

The modified client:

class ClientActor extends Actor {
  def receive = {
    case "Ask" =>{
      val actor = remote.actorFor(
        "hello-service", "localhost", 2552)
      actor ! "Hello"
    }
    case response:String => println(response) // This executed on the server! That's not what I expect?
  }
}

object Client extends App {
  // client code
  val client = actorOf[ClientActor].start();
  client ! "Ask"
}

What am I missing? Is that the expected behavior of Akka? And how can I force it to send the answer back to the client?

Thanks for any input.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

将军与妓 2024-12-15 09:26:16

您尚未在客户端上启动远程处理。

You haven't started the remoting on the client.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文