Scala Remote Actors 阻止客户端终止

发布于 2024-10-01 22:16:37 字数 978 浏览 0 评论 0原文

我正在编写一个简单的聊天服务器,我想让它尽可能简单。下面列出的我的服务器仅接收连接并将它们存储在客户端集中。然后传入的消息被广播到该服务器上的所有客户端。服务器工作没有问题,但在客户端,RemoteActor 阻止我的程序终止。有没有办法在不终止服务器上的 Actor 的情况下删除客户端上的 Actor?

我还不想使用“每个客户一个演员”的模型。

 import actors.{Actor,OutputChannel}
 import actors.remote.RemoteActor

 object Server extends Actor{
  val clients = new collection.mutable.HashSet[OutputChannel[Any]]
  def act{
   loop{
    react{
     case 'Connect =>
      clients += sender
     case 'Disconnect =>
      clients -= sender
     case message:String =>
      for(client <- clients)
      client ! message
    }
   }
  }

  def main(args:Array[String]){
   start
   RemoteActor.alive(9999)
   RemoteActor.register('server,this)
  }
 }

我的客户会看起来像这样

val server = RemoteActor.select(Node("localhost",9999),'server)
server.send('Connect,messageHandler) //answers will be redirected to the messageHandler
/*do something until quit*/
server ! 'Disconnect

I am writing a simple chat server, and I want to keep it as simple as possible. My server listed below only receives connections and stores them in the clients set. Incoming messages are then broadcasted to all clients on that Server. The server works with no problem, but on the client side, the RemoteActor stops my program from termination. Is there a way to remove the Actor on my client without terminating the Actor on the Server?

I don't want to use a "one actor per client" model yet.

 import actors.{Actor,OutputChannel}
 import actors.remote.RemoteActor

 object Server extends Actor{
  val clients = new collection.mutable.HashSet[OutputChannel[Any]]
  def act{
   loop{
    react{
     case 'Connect =>
      clients += sender
     case 'Disconnect =>
      clients -= sender
     case message:String =>
      for(client <- clients)
      client ! message
    }
   }
  }

  def main(args:Array[String]){
   start
   RemoteActor.alive(9999)
   RemoteActor.register('server,this)
  }
 }

my client would then look like this

val server = RemoteActor.select(Node("localhost",9999),'server)
server.send('Connect,messageHandler) //answers will be redirected to the messageHandler
/*do something until quit*/
server ! 'Disconnect

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

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

发布评论

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

评论(1

赴月观长安 2024-10-08 22:16:38

我建议将客户端代码放入演员本身 - 即不在主线程中调用alive/register

(由 http://www.scala-lang.org/api/current/scala/actors/remote/RemoteActor$.html)

类似

//body of your main: 
val client = actor { 
   alive(..)
   register(...)
   loop { 
       receive {
           case 'QUIT => exit()
       }
    } 
}
client.start
//then to quit:
client ! 'QUIT

或类似(抱歉,我没有使用 2.8,所以可能搞砸了一些东西 - 如果你让它真正适合你,请随意编辑!)。

I would suggest placing the client side code into an actor itself - ie not calling alive/register in the main thread

(implied by http://www.scala-lang.org/api/current/scala/actors/remote/RemoteActor$.html)

something like

//body of your main: 
val client = actor { 
   alive(..)
   register(...)
   loop { 
       receive {
           case 'QUIT => exit()
       }
    } 
}
client.start
//then to quit:
client ! 'QUIT

Or similar (sorry I am not using 2.8 so might have messed something up - feel free to edit if you make it actually work for you !).

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