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.
source share