使用Scala和Akka IO有一种方法可以让一个Actor严格用于监听,然后在建立连接时创建一个新的actor,然后负责该Socket(阅读,写作等)?
到目前为止,我有这个.问题是Server actor正在接收数据.我想将套接字的所有权转移到新创建的Client actor,以便它接收与套接字相关的任何消息.谁知道怎么做?
编辑:添加解决方案.我只需要将ActorRef传递给accept的curried参数
import akka.actor._
import akka.actor.IO.SocketHandle
import java.net.InetSocketAddress
/**
* Purpose:
* User: chuck
* Date: 17/01/13
* Time: 5:37 PM
*/
object Main {
class Server extends Actor {
override def preStart() {
IOManager(context.system) listen new InetSocketAddress(3333)
}
def receive = {
case IO.NewClient(server) =>
val client = context.actorOf(Props(new Client()))
server.accept()(client)
println("Client accepted")
case IO.Read(socket, bytes) =>
println("Server " + bytes)
}
}
class Client() extends Actor {
def receive = {
case IO.Read(socket, bytes) …Run Code Online (Sandbox Code Playgroud) 所以我开始学习Erlang,我对这段代码感到困惑.
-module(prior).
-compile(export_all).
important() ->
receive
{ Priority, Msg } when Priority > 10 ->
[Msg | important()]
after 0 ->
normal()
end.
normal() ->
receive
{ _, Msg } ->
[Msg | normal()]
after 0 ->
[]
end.
Run Code Online (Sandbox Code Playgroud)
我正在调用代码.
10> self() ! {15, high}, self() ! {7, low}, self() ! {1, low}, self() ! {17, high}.
{17,high}
11> prior:important().
[high,high,low,low]
Run Code Online (Sandbox Code Playgroud)
据我所知,此代码将首先通过所有高优先级消息,然后是低优先级消息.我很困惑返回值是如何[高,高,低,低],因为我没有看到它们在哪里连接在一起.