3

我正在尝试使用 Akka 远程演员,试图通过网络建立一个简单的 2 人游戏。这个对先前问题的回答给了我一个很好的起点,但现在我很难理解如何适应我的情况。ISTM 原件从同一个客户端连接两次(见下面的评论部分)。我想要做的是从不同的客户端运行它两次,但是当我这样做时,我得到一个 BindException,地址已经在使用中。我想那是因为每次我运行代码时它都会尝试启动服务器?我需要一种情况,我可以独立于客户端连接和断开来启动和停止主参与者。(最小的)Akka 配置和异常在代码之后:

import akka.actor._
//example from answer to https://stackoverflow.com/questions/15527193/keeping-references-to-two-actors
// by Patrick Nordwall
case object JoinMsg
case class Msg(s: String)

class Server extends Actor {

  def receive = {
    case JoinMsg =>
      println("got player 1")
      sender ! Msg("Waiting for player 2")
      context.become(waitingForPlayer2(sender))
  }

  def waitingForPlayer2(client1: ActorRef): Actor.Receive = {
    case JoinMsg =>
      println("got player 2")
      sender ! Msg("hi")
      client1 ! Msg("hi")
      context.become(ready(client1, sender))
  }

  def ready(client1: ActorRef, client2: ActorRef): Actor.Receive = {
    case m: Msg if sender == client1 => client2 ! m
    case m: Msg if sender == client2 => client1 ! m
  }
}

/* I want to run this once for each "player" */
object Demo extends App {
  val system = ActorSystem("Game")
  val server = system.actorOf(Props[Server], "server")

  system.actorOf(Props(new Actor {
    server ! JoinMsg
    def receive = {
      case Msg(s) => println(s)
    }
  }))

  /* Rather than connecting twice from the same node, I want to run this
     program twice from different nodes 
    system.actorOf(Props(new Actor {
    server ! JoinMsg
    def receive = {
      case Msg(s) => println(s)
    }
  }))*/
}

配置:

akka {
  actor {
    provider = "akka.remote.RemoteActorRefProvider"
  }
  remote {
    transport = "akka.remote.netty.NettyRemoteTransport"
    netty {
      hostname = "localhost"
      port = 9000
    }
 }
}

例外:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at akkademo.main(akkademo.scala)
Caused by: org.jboss.netty.channel.ChannelException: Failed to bind to: localhost/127.0.0.1:9000
    at org.jboss.netty.bootstrap.ServerBootstrap.bind(ServerBootstrap.java:298)
    at akka.remote.netty.NettyRemoteServer.start(Server.scala:54)
    at akka.remote.netty.NettyRemoteTransport.start(NettyRemoteSupport.scala:90)
    at akka.remote.RemoteActorRefProvider.init(RemoteActorRefProvider.scala:94)
    at akka.actor.ActorSystemImpl._start(ActorSystem.scala:588)
    at akka.actor.ActorSystemImpl.start(ActorSystem.scala:595)
    at akka.actor.ActorSystem$.apply(ActorSystem.scala:111)
    at akka.actor.ActorSystem$.apply(ActorSystem.scala:93)
    at akkademo$.<init>(akkademo.scala:4)
    at akkademo$.<clinit>(akkademo.scala)
    ... 1 more
Caused by: java.net.BindException: Address already in use

TIA。

4

3 回答 3

12

在同一台机器上运行多个实例时,您需要为它们配置不同的端口。在此示例中,只有服务器需要知道端口 (9000)。对于客户端,您可以使用 0 作为随机可用端口。

为客户端定义另一个配置文件。client.conf

akka {
  actor {
    provider = "akka.remote.RemoteActorRefProvider"
  }
  remote {
    transport = "akka.remote.netty.NettyRemoteTransport"
    netty {
      hostname = "localhost"
      port = 0
    }
 }
}

ActorSystem使用此配置在客户端中启动:

import com.typesafe.config.ConfigFactory
val system = ActorSystem("Game", ConfigFactory.load("client"))

在客户端中,您将使用以下命令查找服务器:

val server = system.actorFor("akka://Game@localhost:9000/user/server")
于 2013-03-21T13:19:55.903 回答
4

您要么需要单独指定客户端配置的端口,要么将服务器端配置的端口从 2552 更改为其他端口。2552 是 akka actor 系统的默认端口,当您在客户端创建一个 actor 系统进行查找(在同一台机器上)时,如果未明确指定,它也会尝试绑定到端口 2552,因此您会收到此错误。

于 2013-04-09T07:16:16.367 回答
0

另一种更简单的方法可能是关闭所有终端窗口并打开新窗口。即使在停止所有节点后也面临同样的问题,但会在同一个终端中运行而不是打开新的终端

于 2020-08-04T18:14:08.890 回答