В настоящее время я разрабатываю систему чата, но я столкнулся с проблемой, когда мой список друзей клиента не транслируется в список друзей другого клиента, когда я присоединяюсь к серверу.Например, я открываю 2 чата и вводя имя в нем.Но он не отображает имя другого чата в пользовательском интерфейсе просмотра списка, который я создал.
Класс клиента
def receive = {
case StartJoin(name) =>
val serverRef = context.actorOf(Props[Server](), "server")
// val serverRef = context.actorSelection(s"akka.tcp://chat@$server:$port/user/server")
val result = serverRef ? Join(self,name)
result.foreach(x => {
if (x == Client.Joined) {
Platform.runLater {
Chatroom.controller.displayJoinStatus("You have Joined")
}
context.become(joined)
} else {
Platform.runLater {
Chatroom.controller.displayJoinStatus("Error")
}
}
})
case _ =>
}
def joined: Receive = {
case StartJoin(x) =>
Platform.runLater {
Chatroom.controller.displayJoinStatus("You have already Joined")
}
case Members(list) =>
Client.memberList = Option(list.filterNot(x => x.ref == self))
Client.memberIter = Option(Iterator.continually(Client.memberList.get.map(_.ref)).flatten)
Platform.runLater {
Chatroom.controller.displayMemberList(memberList.getOrElse(List()).toList)
}
}
}
object Client {
var memberList: Option[Iterable[Person]] = None
var memberIter: Option[Iterator[ActorRef]] = None
case object Joined
case class StartJoin(name: String)
case class Members(member:Iterable[Person])
}
Класс сервера
case class Person(ref: ActorRef, name: String){
override def toString: String ={
name
}
}
class Server extends Actor{
implicit val timeout: Timeout = Timeout(10 second)
context.system.eventStream.subscribe(self, classOf[akka.remote.DisassociatedEvent])
def receive = {
case Join(my,name) =>
Server.players += new Person(my,name)
sender() ? Joined
case _=>
}
def started: Receive = {
case DisassociatedEvent(local,remote, _) =>
context.unbecome()
Server.players.clear()
case _=>
}
}
object Server{
val players = new ObservableHashSet[Person]
case class Join(myRef: ActorRef, name:String)
}
Класс чата
object Chatroom extends JFXApp{
val isServer = false
val myConf = getConfig(isServer)
val system = ActorSystem("chat", myConf)
// val serverRef = system.actorOf(Props[Server](), "server")
//create client actor
val clientRef = system.actorOf(Props[Client], "client")
val loader = new FXMLLoader(null, NoDependencyResolver)
loader.load(getClass.getResourceAsStream("window.fxml"))
val borderPane: jfsl.BorderPane = loader.getRoot[jfsl.BorderPane]
val controller = loader.getController [WindowsController#Controller]()
controller.clientActorRef = Option(clientRef)
stage = new PrimaryStage{
scene = new Scene(){
root = borderPane
}
}
stage.onCloseRequest = handle{
system.terminate()
}
def getConfig(isServer: Boolean): Config = {
val ipaddress = "127.0.0.1"
val port = isServer match {
case true => 6000
case false => 0
}
val overrideConf = ConfigFactory.parseString(
s"""
|akka {
| loglevel = "INFO"
|
| actor {
| provider = "akka.remote.RemoteActorRefProvider"
| }
|
| remote {
| enabled-transports = ["akka.remote.netty.tcp"]
| netty.tcp {
| hostname = "${ipaddress}"
| port = ${port}
| }
|
| log-sent-messages = on
| log-received-messages = on
| }
|
|}
|
""".stripMargin)
val myConf = overrideConf.withFallback(ConfigFactory.load())
return myConf
}
}