我正在试验Akka远程演员,试图通过网络建立一个简单的2人游戏。 This answer先前的问题给了我一个非常好的起点,但现在我很难理解如何使其适应我的情况。 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。
答案 0 :(得分:11)
在同一台计算机上运行多个实例时,需要为它们配置不同的端口。 在此示例中,只有服务器需要知道端口(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")
答案 1 :(得分:4)
您需要单独指定客户端配置的端口,或者将服务器端配置的端口从2552更改为其他端口。 2552是akka actor系统的默认端口,当您在客户端创建一个actor系统进行查找时(在同一台机器上),如果未明确指定,它也会尝试绑定到端口2552,从而导致此错误。
答案 2 :(得分:0)
另一种更简便的方法是关闭所有终端窗口并打开新的窗口。即使在停止所有节点之后也面临着相同的问题,但是会在同一终端中运行,而不是打开新节点