简体   繁体   中英

Scala / Play2 - WebSocket Enumerator and Actor

I have a web page to submit long running jobs. Submitting a job will provide a link to a detailed job view, containing log messages that occur when running the job. I wan't the messages to be displayed when they are available without having to reload the page.

Therefor I create a WebSocket connection using JavaScript and a corresponding route. My idea was to use actors (akka) to pass the messages from the server side to the client side.

How can I achieve this using scala 2.11 and play 2.3.4?

best regards!

Here you get a detailed description: https://www.playframework.com/documentation/2.3.x/ScalaWebSockets

Here is a rough description based on your requirements

First you create an action using WebSocket.acceptWithActor

case class MyMessage(s: String)

def socket = WebSocket.acceptWithActor[MyMessage, JsValue] { request => out =>
  MyWebSocketActor.props(out)
}

object MyWebSocketActor {
   def props(out: ActorRef) = Props(new MyWebSocketActor(out))
}

class MyWebSocketActor(out: ActorRef) extends Actor {
  def receive = {
    case msg: MyMessage =>
     out ! Json.toJson(msg)
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM