无法在Play 2.5 websocktes中获得枚举器的值

时间:2016-06-13 07:48:35

标签: scala playframework websocket

package controllers

import javax.inject._
import play.api._
import play.api.mvc._
import play.api.libs.json._
import play.api.libs.streams._
import akka.stream._
import akka.actor._
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.ActorRef
import akka.actor.Props
import akka.pattern.ask
import akka.util.Timeout
import akka.actor.PoisonPill
import scala.concurrent.duration._
import akka.stream.Materializer
import play.api.cache._
import play.api.libs.iteratee._
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.concurrent._
import play.api.libs.ws.WSClient
/**
 * This controller creates an `Action` to handle HTTP requests to the
 * application's home page.
 */
@Singleton
class HomeController @Inject() (cache:CacheApi) (implicit actorSystem:ActorSystem , materializer:Materializer) extends Controller {


  def validate(receivedMsg:JsValue,outChannel:Concurrent.Channel[JsValue], privateChannel:Concurrent.Channel[JsValue],outEnumerator:Enumerator[JsValue],privateEnumerator:Enumerator[JsValue]) = {

    val user_key = (receivedMsg \ "username").get
    val username = user_key.toString().stripSuffix("\"").stripPrefix("\"")

    val validate_user :Option[String] = cache.get[String](username)

        val valid_result = validate_user.toString()

        if(valid_result.equals("None")) {
          //cache is not set
         // println(valid_result)
          //add user
          cache.set(username,username)
          Ok.withSession(username->username)

          //notify all users
          val successMsg = Json.parse("""{"username":"Server","message":"A new user has been connected"}""") 

          outChannel.push(successMsg)
          (outEnumerator)


        }
        else{
          //cache is already set 
          //send error msg to new user 

           val errorMsg = Json.parse("""{"username":"Server","message":"This username is already taken"}""") 



          // val (privateEnumerator,privateChannel) = Concurrent.broadcast[JsValue]
           privateChannel.push(errorMsg)

           (privateEnumerator)


        }




  }


 val (outEnumerator,outChannel) = Concurrent.broadcast[JsValue]; //public stuff

  def socket = WebSocket.using[JsValue] {


  request => {



   val (privateEnumerator,privateChannel) = Concurrent.broadcast[JsValue]

   var enumerator = privateEnumerator
    var ret = 1;
    val inIteratee: Iteratee[JsValue, Unit] = Iteratee.foreach[JsValue](receivedMsg => {



    enumerator = validate(receivedMsg,outChannel,privateChannel,outEnumerator,privateEnumerator) 



    })

    (inIteratee, enumerator)
  }
}


}

我是scala和Play webSockets的新手。我正在玩2.5.3。以上取决于具体情况,我试图获得私人频道或公共频道的调查员(即所有连接用户)。但即使它正确地返回它,我也无法在这里(iteratee,enumerator)。我做错了什么?

1 个答案:

答案 0 :(得分:0)

这篇文章的第二个答案(Broadcasting messages in Play Framework WebSockets)做同样的事情。

创建一个说UserManagerActor的演员,其唯一目的是管理用户并维护他们。

UserManagerActor来电广播。

val (enumerator, channel) = Concurrent.broadcast[String]

channel有助于使用push方法向所有用户广播消息。

现在,演员可以管理地图中的用户状态

val users = Map[String, (Enumerator[String],Channel[String])]()

一旦所有用户断开连接,确保演员被杀死。

使用Iteratee了解用户是否已断开连接

同时删除断开连接的用户以保持Map的大小可管理。