Scala Play Framework-手动升级到Web套接字

Scala Play Framework-手动升级到Web套接字,scala,websocket,playframework,Scala,Websocket,Playframework,升级到web套接字在play框架中进行了抽象。如何手动实现 我正在使用play框架创建一个web套接字应用程序。 我可以在web套接字客户端和服务器(我创建的)之间建立连接 但升级到WebSocket已经被play框架抽象了 def chatSystem(): WebSocket = WebSocket.acceptOrResult[String, String] { request => Future.successful{ AuthenticationService

升级到web套接字在play框架中进行了抽象。如何手动实现

我正在使用play框架创建一个web套接字应用程序。 我可以在web套接字客户端和服务器(我创建的)之间建立连接 但升级到WebSocket已经被play框架抽象了

def chatSystem(): WebSocket = WebSocket.acceptOrResult[String, String] { request =>
    Future.successful{
      AuthenticationService.doBasicAuthentication(request.headers) match {
        case Results.Ok => Right(ActorFlow.actorRef { out => ChatServiceActor.props(out) })
        case _ => Left(Unauthorized)
      }
    }
  }

URL : ws://localhost:9000/chatSystem/test123
在建立连接之前,我需要做以下事情

如果Websocket服务器无法识别URL路径中的用户ID,则应发送状态为404的HTTP响应并中止Websocket连接

如果Websocket服务器不同意使用客户端提供的其中一个子目录,则它必须完成Web套接字握手,并给出不带Sec Websocket协议头的响应,然后立即关闭Websocket连接

我不知道如何手动实现逻辑,以便向客户端发送升级响应,然后建立web套接字
连接。

遗憾的是,我认为这是不可能的,我能够检测到
WebSocket
处理程序
,play希望实现路由的方法返回
处理程序
,因此,理论上,如果您可以构建一个方法来返回一个
处理程序
,该处理程序是一个将
请求头
转换为另一个
处理程序
(例如
trait CustomHandler extends(RequestHeader=>Handler)的函数,那么,您可以创建一个实例来检查是否应该建立web套接字连接

但是,如果您这样做,在启动请求时会出现异常,希望有一个关键的细节我遗漏了,我建议在他们的github上打开一个票证:

我的目标是:

akka.http.impl.util.One2OneBidiFlow$OutputTruncationException: Inner flow was completed without producing result elements for 1 outstanding elements
    at akka.http.impl.util.One2OneBidiFlow$OutputTruncationException$.apply(One2OneBidiFlow.scala:22)
    at akka.http.impl.util.One2OneBidiFlow$OutputTruncationException$.apply(One2OneBidiFlow.scala:22)
    at akka.http.impl.util.One2OneBidiFlow$One2OneBidi$$anon$1$$anon$4.onUpstreamFinish(One2OneBidiFlow.scala:97)
    at akka.stream.impl.fusing.GraphInterpreter.processEvent(GraphInterpreter.scala:504)
    at akka.stream.impl.fusing.GraphInterpreter.execute(GraphInterpreter.scala:378)
    at akka.stream.impl.fusing.GraphInterpreterShell.runBatch(ActorGraphInterpreter.scala:588)
    at akka.stream.impl.fusing.GraphInterpreterShell$AsyncInput.execute(ActorGraphInterpreter.scala:472)
    at akka.stream.impl.fusing.GraphInterpreterShell.processEvent(ActorGraphInterpreter.scala:563)
    at akka.stream.impl.fusing.ActorGraphInterpreter.akka$stream$impl$fusing$ActorGraphInterpreter$$processEvent(ActorGraphInterpreter.scala:745)
    at akka.stream.impl.fusing.ActorGraphInterpreter$$anonfun$receive$1.applyOrElse(ActorGraphInterpreter.scala:760)
    at akka.actor.Actor.aroundReceive(Actor.scala:517)
    at akka.actor.Actor.aroundReceive$(Actor.scala:515)
    at akka.stream.impl.fusing.ActorGraphInterpreter.aroundReceive(ActorGraphInterpreter.scala:670)
    at akka.actor.ActorCell.receiveMessage(ActorCell.scala:588)
    at akka.actor.ActorCell.invoke(ActorCell.scala:557)
    at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:258)
    at akka.dispatch.Mailbox.run(Mailbox.scala:225)
    at akka.dispatch.Mailbox.exec(Mailbox.scala:235)
    at akka.dispatch.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
    at akka.dispatch.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
    at akka.dispatch.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
    at akka.dispatch.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
akka.http.impl.util.One2OneBidiFlow$OutputTruncationException: Inner flow was completed without producing result elements for 1 outstanding elements
    at akka.http.impl.util.One2OneBidiFlow$OutputTruncationException$.apply(One2OneBidiFlow.scala:22)
    at akka.http.impl.util.One2OneBidiFlow$OutputTruncationException$.apply(One2OneBidiFlow.scala:22)
    at akka.http.impl.util.One2OneBidiFlow$One2OneBidi$$anon$1$$anon$4.onUpstreamFinish(One2OneBidiFlow.scala:97)
    at akka.stream.impl.fusing.GraphInterpreter.processEvent(GraphInterpreter.scala:504)
    at akka.stream.impl.fusing.GraphInterpreter.execute(GraphInterpreter.scala:378)
    at akka.stream.impl.fusing.GraphInterpreterShell.runBatch(ActorGraphInterpreter.scala:588)
    at akka.stream.impl.fusing.GraphInterpreterShell$AsyncInput.execute(ActorGraphInterpreter.scala:472)
    at akka.stream.impl.fusing.GraphInterpreterShell.processEvent(ActorGraphInterpreter.scala:563)
    at akka.stream.impl.fusing.ActorGraphInterpreter.akka$stream$impl$fusing$ActorGraphInterpreter$$processEvent(ActorGraphInterpreter.scala:745)
    at akka.stream.impl.fusing.ActorGraphInterpreter$$anonfun$receive$1.applyOrElse(ActorGraphInterpreter.scala:760)