Play Framework as WebSocket client

I am using Play 2.3, checking the documentation for using Akka as a WebSocket server. However, they did not specify in the documentation whether Play can connect to existing WebSocket servers. I am mainly interested in being a WebSocket client that will receive messages from the WebSocket server. The My Play app will make a bi-directional request / response with this WebSocket server and then finally I start shutting down.

Is this possible with playing with Akka?

+3


source to share


2 answers


Not the way it is. It currently acts only as a server.



What you can do is use a client like jetty websocket from within the game itself and then process the data as you see fit.

+4


source


Checkout http://backchatio.github.io/hookup/



import io.backchat.hookup._

new DefaultHookupClient(HookupClientConfig(new URI("ws://localhost:8080/thesocket"))) {

  def receive = {
    case Disconnected(_) β‡’ 
      println("The websocket to " + uri.toASCIIString + " disconnected.")
    case TextMessage(message) β‡’ {
      println("RECV: " + message)
      send("ECHO: " + message)
    }
  }

  connect() onSuccess {
    case Success β‡’
      println("The websocket is connected to:"+this.uri.toASCIIString+".")
      system.scheduler.schedule(0 seconds, 1 second) {
        send("message " + messageCounter.incrementAndGet().toString)
      }
    case _ β‡’
  }
}

      

0


source







All Articles