我正在尝试使用 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。