首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Play Framework -手动升级到Web socket

Play Framework -手动升级到Web socket
EN

Stack Overflow用户
提问于 2019-07-12 19:39:15
回答 1查看 353关注 0票数 3

升级到web socket是在play frame中抽象出来的。如何手动实现?

我使用play框架创建了一个web socket应用程序。我可以在web socket客户端和服务器(我创建的)之间建立连接,但是升级到web socket已经被play-framework抽象出来了。

代码语言:javascript
复制
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路径中的userId,它应该发送一个状态为404的超文本传输协议响应并中止WebSocket连接

如果WebSocket服务器不同意使用客户端提供的某个子协议,它必须使用不带Sec-WebSocket-Protocol报头的响应完成WebSocket握手,然后立即关闭WebSocket连接。

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

EN

回答 1

Stack Overflow用户

发布于 2019-07-13 13:24:02

可悲的是,我认为这是不可能的,我能够检测到WebSocket是一个Handler,而play期望实现路由的方法返回一个Handler,所以,从理论上讲,如果你可以构建一个返回Handler的方法,该方法是一个将RequestHeader转换为另一个Handler (如trait CustomHandler extends (RequestHeader => Handler) with Handler)的函数,那么,你就可以创建一个实例来检查是否应该建立web套接字连接。

但是,如果你这样做,你会在触发请求时得到一个异常,希望有一个我遗漏的关键细节,我建议在他们的github上打开一个工单:

我的堆栈跟踪是:

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57006270

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档