I've written a client and server using akka.io.tcp and I'm having a problem when reading the messages sent by the client. I've used json to send the messages. In the client side, I write the message this way:
connection ! Write(ByteString(msgString))
In the server side I have the following:
override def receive: Receive = {
case Received(data) => listener ! Json.parse(data.utf8String)
case PeerClosed => {
context stop self
}
}
The problem is akka is reading more than one message at a time, so I get an invalid Json. Is there a way to make akka read just one message at a time?
Akka's TCP module is (and is designed to be) very "low level", so we're not providing any kind of frame delimiters. You should treat it more like an TCP level building block you then have to build your stuff on.
In your example, the Actor would have to aggregate the incoming chunks of data, and detect when a json document is "complete", then trigger the unmarshalling.
The idea of providing something out of the box for this is quite compelling... I'll ask the guys what our plans are for supporting these kinds of use cases.
Still, I hope this helped!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With