Akka TCP client: how can I send a message over TCP using akka actor

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

This article presents a simple implementation of a TCP client, but it is not clear to me how I will use this client.

  • The constructor accepts InetSocketAddress and ActorRef. InetSocketAddress makes sense (I assume this is the destination), but what is an ActorRef? This is my first time using Accu, but from what I understand, ActorRef is a link from another actor. Since my TCP client is an actor, and I expect this TCP user to interact with the TCP server and not with another actor, why should I give it a ref actor?

  • What is the props function in the companion object for?

  • after creating the instance, how to use this actor to send TCP messages? Should I just send him a message with the data I want to send in the ByteString form?

4. what is the connection / difference between

case Received(data) => listener ! data 

and

 case data: ByteString => connection ! Write(data) 
+5
source share
1 answer

Answering your questions:

  • The constructor of the class Client(remote: InetSocketAddress, listener: ActorRef) extends Actor accepts a listener , which is a reference to the actor who uses this Client to communicate with the remote server. The listener will send and receive messages through this Client . Since Client is an actor, you will only communicate with him by sending messages. The same applies to Client when it communicates with connection / remote - it will send and receive messages on your behalf and forward them to the listener that you provided. Function
  • props in a companion actor class object is usually used as an auxiliary function to build an actor. This is necessary if your actor accepts the arguments of the constructor, and you need to take care not to close the volatile state. Remember that you cannot use the new operator to create participants, you must call props .
  • The Client actor will receive messages of the ByteString type, as in case data: ByteString => after connecting to the remote control. It will record this data in a TCP connection - effectively sending a message. Whenever it receives a response from a remote Received type, as in case Received(data) => , it passes it to your listener actor.
  • See 3. Client Actor receives messages from your listener and from connection and forwards them accordingly. However, he does not check where they came from. Therefore, whenever it receives a ByteString , it sends it to connection / remote, and whenever it receives Received , it sends bytes to the listener . You must ensure that your listener can receive these messages if you want to have two-way communication.

To summarize what a two-way communication looks like.

Send the ByteString to the remote:

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

Get ByteString from a remote server (the server is negotiating with the client):

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

where '->' - sending a message.

+7
source

Source: https://habr.com/ru/post/1236103/


All Articles