Hi,
I need to read messages from clients(web socket clients) and persist these
messages into two files. But, messages are overriding to previous messages.
Every time only last message is present in the file.
How to append the messages. Please help me to resolve this issue.
*Code :- *
def route(implicit actorSystem: ActorSystem, materializer: Materializer):
Route =
pathPrefix("ssys" / LongNumber) { id =>
parameter('name) { name ⇒
handleWebSocketMessages(broadcast(id, name))
}
}
def broadcast(id: Long, name: String): Flow[Message, Message, Any] = {
Flow[Message].collect {
case TextMessage.Strict(text) =>
dump(text, longToIP(id))
TextMessage(s"I got your message: $text!")
}
}
def dump(text: String, id: String ) {
val value1 = Source.single(text, id)
val sink1 = lineSink("/Users/log/data/test1.txt")
val sink2 = lineSink("/Users/log/data/test2.txt")
val g = RunnableGraph.fromGraph(GraphDSL.create() { implicit b =>
import GraphDSL.Implicits._
val bcast = b.add(Broadcast[String](2))
value1.map(x => x) ~> bcast.in
bcast.out(0) ~> sink1
bcast.out(1) ~> sink2
ClosedShape
})
g.run()
}
def longToIP(long: Long): String = {
(0 until 4).map(a => long / math.pow(256, a).floor.toInt %
256).reverse.mkString(".")
}
def lineSink(filename: String): Sink[String, Future[IOResult]] = {
Flow[String]
.map(s => ByteString(s + "\n"))
.toMat(FileIO.toFile(new File(filename)))(Keep.right)
}
Regards,
Rajesh
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ:
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.