播放scala - websocket多线程

时间:2015-10-01 05:42:54

标签: multithreading scala websocket playframework-2.3

目的:

使用play framework scala语言在web-socket方法中添加多线程。

我做了什么:

我已经使用play framework给出的play-websocket示例在客户端/服务器端创建了web-socket。 在Web套接字消息中,我从客户端传递了base64图像,并在服务器端接收了它。

我想要的是什么:

我必须使用多线程将base64映像传递给4种不同的方法,并在Web套接字响应中传递结果消息。

任何人都可以告诉我,是否可以这样做?如果是的话,你能说出如何实现这个吗?这对我有帮助。

代码段:

index.scala.html:

    <ul>
        <li id="open">
            <a href='#'>Open WebSocket</a>
        </li>
        <li id="sendMsg" class="disabled">
            <a href='#'>Send echo</a>
        </li>
        <li id="file" class="disabled">
            <input type="file" id="picture" />
        </li>
    </ul>

@ws("WS_Prova")
<script type="text/javascript" charset="utf-8">
    $(function() {

        WS_Prova.address = "ws://localhost:9000/ws"

            WS_Prova.on('open', function(e) {
                $('#status').text("WebSocket: opened").addClass('opened');
            })

            WS_Prova.on('message', function(e) {
                $('#message').text("Received: " + JSON.stringify(e.content));
            })

            WS_Prova.on('close', function(e) {
                $('#status').text("WebSocket: closed").addClass('closed');
                $('#message').text("...");

            })
            $('#picture').change(function(e) {
                e.preventDefault();
                var reader = new FileReader;
                reader.onloadend = function () {
                    var obj = {
                        "echo" : false,
                        "data" : reader.result
                    }

                    WS_Prova.send(obj);
                }
                reader.readAsDataURL(this.files[0]);
            })
    });

Application.scala:

  def index = Action {implicit request =>
    Ok(views.html.index("Welcome! Let's Play with your brand new websocket!")).withSession(
        ("uuid" -> java.util.UUID.randomUUID.toString)
      )
  }

  def ws = 
    WebSocket.async[JsValue] {implicit request => 
    println("ws call initiated");
      actors.ws.Ws.control
  }
}

Ws.scala:

object Ws extends WebSocketManager[Ws]  {
  case class AlertOnlyMe(uuid: String)
}

class Ws extends WSManagerActor {

  import Ws._
  import WSClientMsgs._

  override def operative(implicit request: RequestHeader) = {
    (wsClient) => { 
    case AlertOnlyMe(uuid) =>
      for {
        clientUUID <- request.session.get("uuid")
        if (clientUUID.compareTo(uuid) == 0)
      } {
        wsClient ! JsToClient(Json.obj(
              "status" -> true,
          "message" -> "success"
            ))
      }
    case JsFromClient(something) =>
      ((something\"echo").asOpt[Boolean]) match {
        case Some(true) =>
          wsClient ! AlertOnlyMe(request.session.get("uuid").getOrElse(""))
        case _ => 
          println((something\"data"))
          var img = something\"data";
          fn1(img.toString());
          fn2(img.toString());
          fn3(img.toString());
          fn4(img.toString());
          self ! JsToClient(Json.obj("broadcast" -> (something\"data")))
      }
  }
  }

  def fn1(img: String) {

  }

  def fn2(img: String) {

  }

  def fn3(img: String) {

  }

  def fn4(img: String) {

  }

}

0 个答案:

没有答案