Akka TCP客户端:如何使用Akka actor通过TCP发送消息 [英] Akka TCP client: How can I send a message over TCP using akka actor

查看:304
本文介绍了Akka TCP客户端:如何使用Akka actor通过TCP发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过TCP发送文本消息。满容易。我想和akka一起做。我阅读了有关akka IO的文章:
http:/ /doc.akka.io/docs/akka/snapshot/scala/io-tcp.html

I want to send textual messages over TCP. Pretty easy. I want to do so with akka. I read this article about akka IO: http://doc.akka.io/docs/akka/snapshot/scala/io-tcp.html

本文介绍了TCP客户端的简单实现,但我不清楚如何使用此客户端。

The article present a simple implementation of a TCP client, but it's not clear to me how I would use this client.


  1. 构造函数使用一个InetSocketAddress和一个ActorRef。 InetSocketAddress很有意义(我想这是目的地的地址),但是ActorRef是什么?这是我第一次使用akka,但据我了解,ActorRef是另一位演员的参考。由于我的TCP客户端是一个actor,并且我希望这个TCP actor与TCP服务器而不是与另一个actor通信,所以为什么要给它一个actor ref?

  1. The constructor takes an InetSocketAddress and an ActorRef. InetSocketAddress make sense (I assume this is the of the destination) but what's the ActorRef? this is my first time using akka, but from what I understand, ActorRef is the reference of another actor. Since my TCP client is an actor, and I expect this TCP actor to communicate with a TCP server, not with another actor, why would I give it an actor ref?

伴随对象中的props函数有什么作用?

what's the props function in the companion object for?

一旦实例化,我将如何使用此actor发送TCP消息?我应该以ByteString的形式向其发送包含要发送的数据的消息吗?

once instantiated, how would I use this actor to send TCP messages? Should I just send it a message with the data I want to send in the form of a ByteString?

4。
有什么联系/区别

4. what's the connection / difference between

case Received(data) => 
    listener ! data

case data: ByteString =>
    connection ! Write(data)


推荐答案

回答您的问题:


  1. 构造函数类Client(远程:InetSocketAddress,侦听器:ActorRef)扩展Actor 接受 listener 是对使用此 Client 与远程服务器通信的演员的引用。侦听器将通过此 Client 发送和接收消息。由于 Client 是一个参与者,您将仅通过发送消息与其进行通信。当客户端连接 /远程进行通信时,也是如此-它会代表您发送和接收消息,并且将它们转发给您提供的侦听器

  2. props 函数演员类的对象通常用作构造演员的辅助函数。如果演员要使用构造函数参数,并且必须注意不要关闭可变状态,则需要这样做。请记住,您不能使用 new 运算符来创建actor,而必须调用 Props

  3. 您的 Client actor将收到 ByteString 类型的消息,如案例数据:ByteString => 一旦连接到遥控器。它将数据写入TCP连接-有效发送消息。每当它收到来自类型为 Received 的远程服务器的响应时,在 case Received(data)=> 的情况下,它将转发它发送给您的侦听器演员。

  4. 请参阅3。客户端演员从接收消息您的侦听器并从连接进行转发。但是,它不会检查它们来自何处。因此,每当它收到 ByteString 时,它将把它发送到 connection / remote,并且每当它收到收到,它将把字节发送到监听器。如果您想进行两种方式的通信,则需要确保您的听众可以接收这些消息。

  1. The constructor class Client(remote: InetSocketAddress, listener: ActorRef) extends Actor takes a listener which is a reference to an actor that is using this Client to communicate with remote server. Listener will send and receive messages through this Client. Since Client is an actor you will communicate with it solely by sending messages. The same applies to the Client when it communicates with connection/remote - it will send and receive messages on your behalf and forward them to the listener you provided.
  2. props function in a companion object of an actor class is usually used as a helper function for constructing an actor. It's needed if your actor takes constructor arguments and you have to take care not to close over mutable state. Remember you can't use new operator to create actors, you have to call Props.
  3. Your Client actor will receive messages of type ByteString as in case data: ByteString => once connected to the remote. It will write that data to the TCP connection - effectively sending a message. Whenever it receives a response from remote of type Received as in case Received(data) => it will forward it to your listener actor.
  4. See 3. Client actor receives messages from your listener and from connection and forwards them accordingly. It does not check however where they came from. So whenever it receives ByteString it will send it to connection/remote, and whenever it receives Received it will send bytes to listener. You need to make sure your listener can receive these messages if you want to have 2 way communication.

总结一下,这是2-

ByteString 发送到远程:

MyActor-> ByteString ->客户端-> Write(ByteString)-> connection / remote

MyActor -> ByteString -> Client -> Write(ByteString) -> connection/remote

从远程(服务器与客户端对话)接收 ByteString

Receive ByteString from remote (server talks to client):

连接/远程-> Received(ByteString)-> Client-> ByteString -> MyActor

connection/remote -> Received(ByteString) -> Client -> ByteString -> MyActor

其中'->'是消息发送。

where '->' is a message send.

这篇关于Akka TCP客户端:如何使用Akka actor通过TCP发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆