Akka TCP客户端:如何使用Akka actor通过TCP发送消息 [英] Akka TCP client: How can I send a message over TCP using akka actor
问题描述
我想通过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.
-
构造函数使用一个InetSocketAddress和一个ActorRef。 InetSocketAddress很有意义(我想这是目的地的地址),但是ActorRef是什么?这是我第一次使用akka,但据我了解,ActorRef是另一位演员的参考。由于我的TCP客户端是一个actor,并且我希望这个TCP actor与TCP服务器而不是与另一个actor通信,所以为什么要给它一个actor ref?
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)
推荐答案
回答您的问题:
- 构造函数
类Client(远程:InetSocketAddress,侦听器:ActorRef)扩展Actor
接受listener
是对使用此Client
与远程服务器通信的演员的引用。侦听器将通过此Client
发送和接收消息。由于Client
是一个参与者,您将仅通过发送消息与其进行通信。当客户端
与连接
/远程进行通信时,也是如此-它会代表您发送和接收消息,并且将它们转发给您提供的侦听器
。 -
props
函数演员类的对象通常用作构造演员的辅助函数。如果演员要使用构造函数参数,并且必须注意不要关闭可变状态,则需要这样做。请记住,您不能使用new
运算符来创建actor,而必须调用Props
。 - 您的
Client
actor将收到ByteString
类型的消息,如案例数据:ByteString =>
一旦连接到遥控器。它将数据写入TCP连接-有效发送消息。每当它收到来自类型为Received
的远程服务器的响应时,在case Received(data)=>
的情况下,它将转发它发送给您的侦听器
演员。 - 请参阅3。
客户端
演员从接收消息您的侦听器
并从连接
进行转发。但是,它不会检查它们来自何处。因此,每当它收到ByteString
时,它将把它发送到connection
/ remote,并且每当它收到收到
,它将把字节发送到监听器
。如果您想进行两种方式的通信,则需要确保您的听众可以接收这些消息。
- The constructor
class Client(remote: InetSocketAddress, listener: ActorRef) extends Actor
takes alistener
which is a reference to an actor that is using thisClient
to communicate with remote server. Listener will send and receive messages through thisClient
. SinceClient
is an actor you will communicate with it solely by sending messages. The same applies to theClient
when it communicates withconnection
/remote - it will send and receive messages on your behalf and forward them to thelistener
you provided. 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 usenew
operator to create actors, you have to callProps
.- Your
Client
actor will receive messages of typeByteString
as incase 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 typeReceived
as incase Received(data) =>
it will forward it to yourlistener
actor. - See 3.
Client
actor receives messages from yourlistener
and fromconnection
and forwards them accordingly. It does not check however where they came from. So whenever it receivesByteString
it will send it toconnection
/remote, and whenever it receivesReceived
it will send bytes tolistener
. 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屋!