scala with play:试图通过websocket发送数据时类强制转换异常

iyfamqjs  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(287)

我正在尝试使用scala、play和akka建立一个基本的基于websocket的通知系统。websocket工作得很好,我可以发送东西,回显它,用新创建的case类响应等等。我用case类定义了一个sealed trait来表示各种输入和输出,用定制的写和读来管理输入/输出json对象和添加/删除“type”字段。
但是,当我尝试通过外部操作通过websocket发送数据时,会出现以下奇怪的运行时错误: java.lang.ClassCastException: v1.socket.SocketEvent$ cannot be cast to v1.socket.SocketEvent 以下是一个动作的调用:

WebSocketActor.send(ConnectionRequest("test", "test", "test"), "test")

这是我的websocketactor:

object WebSocketActor {
  def props(out: ActorRef) = Props(new WebSocketActor(out))
  var sessions: Map[String, Map[String, ActorRef]] = Map()

  /**Send data to a specified user. 
    * Response with false if the user does not have a session 
    */
  def send(socketEvent: SocketEvent, userId: String): Boolean = {
    if (sessions contains userId) {
      val userSessions = sessions(userId)
      userSessions.foreach(user => user._2 ! SocketEvent)
      return true
    }
    false
  }
}

class WebSocketActor(out: ActorRef) extends Actor {

  /**Authenticate the user and add device to session
    * (note that authentication is stripped out for brevity) 
    */

  def auth: Receive = {
    case init: Init => {
      var session: Map[String, ActorRef] = Map(init.deviceId -> out)
      if (sessions contains init.userId) {
        session = session ++ sessions(init.userId)
      }
      sessions = sessions + (init.userId -> session)

      context become await
    }
  }

  def await: Receive = {
    case conReq: ConnectionRequest => {
      out ! conReq
    }

  }
  override def receive: Receive = auth

}

下面是socketevent seale trait、伴随对象和case类:

sealed trait SocketEvent

object SocketEvent {

  implicit val socketEventFmt: Format[SocketEvent] = new Format[SocketEvent] {

    override def writes(event: SocketEvent): JsValue = event match {
      case e: ConnectionRequest => Json.toJson(e)(Json.format[ConnectionRequest])
      case e: Init => Json.toJson(e)(Json.format[Init])
    }
    override def reads(json: JsValue): JsResult[SocketEvent] = (json \ "type").as[String] match {
      case "connectionRequest" =>
        Json.fromJson[ConnectionRequest](json.as[JsObject] - "type")(Json.format[ConnectionRequest])
      case "init" =>
        Json.fromJson[Init](json.as[JsObject] - "type")(Json.format[Init])
    }
  }
}

case class ConnectionRequest(userId: String, publicKey: String, signature: String) extends SocketEvent
object ConnectionRequest {
  implicit val format: OWrites[ConnectionRequest] = Json.format[ConnectionRequest]
    .withConstant("type", "connectionRequest")
}

case class Init(userId: String, deviceId: String) extends SocketEvent
object Init{
  implicit val initFmt: OWrites[Init] = Json.format[Init]
    .withConstant("type", "Init")
}

最后,一个用于向输出的json添加字段的owritesextra类:

object OWritesExtra {

  implicit class OWritesExt[A](owrites: OWrites[A]) {

    /**Add a (key, value) pair to the JSON object,
      * where the value is constant.
      */
    def withConstant[B: Writes](key: String, value: B): OWrites[A] =
      withValue(key, _ => value)

    /**Add a key, value pair to the JSON object that is written, where
      * the value depends on the original object.
      */
    def withValue[B: Writes](key: String, value: A => B): OWrites[A] =
      new OWrites[A] {
        def writes(a: A): JsObject = owrites.writes(a) ++ Json.obj(key -> value(a))
      }

  }

}
t5zmwmid

t5zmwmid1#

问题是我在发送 SocketEvent 而不是示例 socketEventsend 方法。

相关问题