Use WebSocket connection using Scala and Play
I am looking for a way to use websocket in Scala.
The web application is created in the playback application. Here's the code in
controller
def socket = WebSocket.acceptWithActor[String, JsValue] { request => out =>
  WebsocketActor.props(out)
}
      
        
        
        
      
    
<strong> routes
# Sockets
GET     /socket                          controllers.MyController.socket
      
        
        
        
      
    
Connecting to a socket using Javascript works great. Now I want to connect to the socket using http-Client. I am currently using PlayHttpClient
Sbt dependecy client
"com.typesafe.play" %% "play-ws" % "2.3.4"
      
        
        
        
      
    
Code for using Websocket
I found several methods on the client that could do the trick, but I am lost on which one to use and how to use them:
https://www.playframework.com/documentation/2.3.x/api/scala/index.html#play.api.libs.ws.WSRequestHolder
def get[A](consumer: (WSResponseHeaders) ⇒ Iteratee[Array[Byte], A])(implicit ec: ExecutionContext): Future[Iteratee[Array[Byte], A]]
      
        
        
        
      
    
looks like it might do the trick. But how to realize the consumer?
def getStream(): Future[(WSResponseHeaders, Enumerator[Array[Byte]])]
// Thats how I used it
  def alertStream = client.
    url(http://localhost:9000/socket).
    withHeaders("Accept" -> "application/json").
    stream.map { case (headers, enumerator) =>
      println(headers)
      val loggingIteratee = Iteratee.foreach[Array[Byte]] { chunk =>
        val chunkString = new String(chunk, "UTF-8")
        println(chunkString)
      }
     enumerator.apply( loggingIteratee )
loggingIteratee.run
      
        
        
        
      
    
}
I tried this, but I keep getting 400-Excetchion in ResponseHeaders. Changing the protocol to ws: // in the url results in this exception:
> val con = client.url("ws://localhost:9000/socket").get
java.io.IOException: WebSocket method must be a GET
at    com.ning.http.client.providers.netty.NettyAsyncHttpProvider.doConnect(NettyAsyncHttpProvider.java:989) 
      
        
        
        
      
     No one has answered this question yet
See similar questions:
or similar: