如何在Play 2.6中优雅地处理无效的websocket请求?

时间:2017-08-25 15:14:39

标签: scala playframework

我们有一个使用websockets的play 2.6应用程序。这很有效,但在我们的错误日志中,我们偶尔会看到以下内容:

[ERROR] [08/25/2017 16:00:44.770] [play-dev-mode-akka.actor.default-dispatcher-35] [akka.actor.ActorSystemImpl(play-dev-mode)] Internal server error, sending 500 response
java.lang.RuntimeException: WebSocket returned for non WebSocket request
    at scala.sys.package$.error(package.scala:27)
    at play.core.server.AkkaHttpServer.executeHandler(AkkaHttpServer.scala:271)
    at play.core.server.AkkaHttpServer.handleRequest(AkkaHttpServer.scala:201)
    at play.core.server.AkkaHttpServer.$anonfun$createServerBinding$3(AkkaHttpServer.scala:107)
    at akka.stream.impl.fusing.MapAsync$$anon$23.onPush(Ops.scala:1172)
    at akka.stream.impl.fusing.GraphInterpreter.processPush(GraphInterpreter.scala:499)
    at akka.stream.impl.fusing.GraphInterpreter.processEvent(GraphInterpreter.scala:462)
    at akka.stream.impl.fusing.GraphInterpreter.execute(GraphInterpreter.scala:368)
    at akka.stream.impl.fusing.GraphInterpreterShell.runBatch(ActorGraphInterpreter.scala:571)
    at akka.stream.impl.fusing.GraphInterpreterShell$AsyncInput.execute(ActorGraphInterpreter.scala:457)
    at akka.stream.impl.fusing.GraphInterpreterShell.processEvent(ActorGraphInterpreter.scala:546)
    at akka.stream.impl.fusing.ActorGraphInterpreter.akka$stream$impl$fusing$ActorGraphInterpreter$$processEvent(ActorGraphInterpreter.scala:725)
    at akka.stream.impl.fusing.ActorGraphInterpreter$$anonfun$receive$1.applyOrElse(ActorGraphInterpreter.scala:740)
    at akka.actor.Actor.aroundReceive(Actor.scala:513)
    at akka.actor.Actcorrectlyor.aroundReceive$(Actor.scala:511)
    at akka.stream.impl.fusing.ActorGraphInterpreter.aroundReceive(ActorGraphInterpreter.scala:650)
    at akka.actor.ActorCell.receiveMessage(ActorCell.scala:527)
    at akka.actor.ActorCell.invoke(ActorCell.scala:496)
    at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:257)
    at akka.dispatch.Mailbox.run(Mailbox.scala:224)
    at akka.dispatch.Mailbox.exec(Mailbox.scala:234)
    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)

稍微调整一下后,如果没有正确的Connection和/或Upgrade标头的HTTP请求,就会发生这种情况。播放源为here

我们希望记录更详细的信息以确定发生这种情况的原因(我们怀疑是头文件剥离防火墙),但不幸的是,当发生错误时,我们的websocket处理程序从未被调用(没有任何内容打印到控制台):

def ws = WebSocketcorrectly.acceptOrResult[String, String] { implicit request =>
    println("Hello world") // Only printed for requests with valid headers
    ...
}

我们如何插入一些可以访问请求标头和IP的日志语句?

0 个答案:

没有答案