伙计们!我正在使用akka 2.2.3并开发简单的tcp服务器应用程序。
工作流程是: 1.客户端连接到服务器2.服务器接受连接,3.服务器发送给客户端消息"Hello!“
在页面http://doc.akka.io/docs/akka/2.2.3/scala/io-tcp.html上,我可以看到如何向请求发送响应消息。但是,在收到一些数据之前,我如何发送消息呢?
如何在不首先接收init.Event的情况下向客户端发送消息?
文档页中的代码:
class AkkaSslHandler(init: Init[WithinActorContext, String, String])
extends Actor with ActorLogging {
def receive = {
case init.Event(data) ⇒
val input = data.dropRight(1)
log.debug("akka-io Server received {} from {}", input, sender)
val response = serverResponse(input)
sender ! init.Command(response)
log.debug("akka-io Server sent: {}", response.dropRight(1))
case _: Tcp.ConnectionClosed ⇒ context.stop(self)
}
}发布于 2014-01-16 07:22:51
您也可以使用init来创建TcpPipelineHandler,当然,您可以始终向该参与者发送命令。为此,除了Init之外,您还需要将其ActorRef传递给处理程序参与者。
https://stackoverflow.com/questions/21145459
复制相似问题