I have created a web socket that receives a single message, that will do some processing and returns the response message to the client. I have created web socket using Play framework. The code snippet is given below.
Code snippet:
def multi_request = WebSocket.tryAccept[String] {
request =>
val (out, channel) = Concurrent.broadcast[String]
val in = Iteratee.foreach[String] {
msg =>
channel push ("message " + msg +" request Time: " + System.currentTimeMillis()/1000)
if(msg.equals("1")) {
Thread.sleep(20000);
println(msg);
} else if(msg.equals("2")) {
Thread.sleep(10000);
println(msg);
} else {
println(msg);
}
channel push ("message " + msg +" response Time: " + System.currentTimeMillis()/1000)
}
Future.successful(Right(in, out))
}
I have tested my web socket from the http://www.websocket.org/echo.html. I have connected my web socket and passed three messages sequentially as "1", 2" and "3". I got the below response while passing these messages.
SENT: 1
RESPONSE: message 1 request Time: 1457351625
SENT: 2
SENT: 3
RESPONSE: message 1 response Time: 1457351645
RESPONSE: message 2 request Time: 1457351646
RESPONSE: message 2 response Time: 1457351656
RESPONSE: message 3 request Time: 1457351656
RESPONSE: message 3 response Time: 1457351656
It seems that, the web socket request hits the server sequentially not In parallel. The three messages sent from the client immediately when I pass it. But it is not hitting server in parallel.
That is, the second request hits after the first response message. The third message hits after the second response message.
Is this the default web socket behaviour?
Or Do I want to implement multi-threading to handle this kind of request in Scala play Framework?
Or Did I miss anything in the code to handle multiple requests from the single client?