js和akka-http websockets之间的连接95%的时间会失败



我正在尝试在akka-http websocket服务器和简单的javascript之间建立一个基本连接。

大约20个连接中有1个成功,其余的失败。我不知道为什么连接设置这么不可靠。

Application.scala:

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer
import services.WebService
import scala.concurrent.Await
import scala.concurrent.duration._
import java.util.concurrent.TimeoutException

object Application extends App {
  implicit val system = ActorSystem("api")
  implicit val materializer = ActorMaterializer()
  import system.dispatcher
  val config = system.settings.config
  val interface = config.getString("app.interface")
  val port = config.getInt("app.port")
  val service = new WebService
  val binding = Http().bindAndHandle(service.route, interface, port)
  try {
    Await.result(binding, 1 second)
    println(s"server online at http://$interface:$port/")
  } catch {
    case exc: TimeoutException =>
      println("Server took to long to startup, shutting down")
      system.shutdown()
  }
}

WebService.scala:

import actors.{PublisherActor, SubscriberActor}
import akka.actor.{Props, ActorSystem}
import akka.http.scaladsl.model.ws.{Message, TextMessage}
import akka.http.scaladsl.server.Directives
import akka.stream.Materializer
import akka.stream.scaladsl.{Source, Flow}
import scala.concurrent.duration._

class WebService(implicit fm: Materializer, system: ActorSystem) extends Directives {
  import system.dispatcher
  system.scheduler.schedule(15 second, 15 second) {
    println("Timer message!")
  }
  def route =
    get {
      pathSingleSlash {
        getFromResource("web/index.html")
      } ~
        path("helloworld") {
          handleWebsocketMessages(websocketActorFlow)
        }
    }
  def websocketActorFlow: Flow[Message, Message, Unit] =
    Flow[Message].collect({
      case TextMessage.Strict(msg) =>
        println(msg)
        TextMessage.Strict(msg.reverse)
    })
}

客户端:

<input type="text" id="inputMessage"/><br/>
<input type="button" value="Send!" onClick="sendMessage()"/><br/>
<span id="response"></span>
<script type="application/javascript">
    var connection;
    function sendMessage() {
        connection.send(document.getElementById("inputMessage").value);
    }
    document.addEventListener("DOMContentLoaded", function (event) {
        connection = new WebSocket("ws://localhost:8080/helloworld");
        connection.onopen = function (event) {
            connection.send("connection established");
        };
        connection.onmessage = function (event) {
            console.log(event.data);
            document.getElementById("response").innerHTML = event.data;
        }
    });
</script>

如果连接到服务器失败,我在5秒后得到一个超时消息,上面写着:

[DEBUG] [07/23/2015 07:59:54.517] [api-akka.actor.default-dispatcher-27] [akka://api/user/$a/flow-76-3-publisherSource-prefixAndTail] Cancelling akka.stream.impl.MultiStreamOutputProcessor$SubstreamOutput@a54778 (after: 5000 ms)

无论连接成功还是失败,我总是得到以下日志消息:

[DEBUG] [07/23/2015 07:59:23.849] [api-akka.actor.default-dispatcher-4] [akka://api/system/IO-TCP/selectors/$a/0] New connection accepted

仔细查看错误信息…它来自一个我没有预料到的来源,当我只期望处理单个流时,一些"MultiStreamOutputProcessor"。

这告诉我-以及webSocketActorFlow -也许你正在接收消息,它们没有被流捕获,所以你最终得到了你从未预料到的子流。

所以它不是"只在某些时间工作",也许它是"大部分时间工作,但无法处理你在流中要求的所有输入,并且你留下了不可选择的流,必须首先死亡。

看看你是否可以a)确保你掌握了流,这样你就不会有掉队者,b) bandaid调整超时,以及c) 检测这种情况并取消处理下游

https://groups.google.com/forum/!主题/akka-user x-tARRaJ0LQ

akka {流{实现{subscription-timeout {timeout = 30年代}}}}

http://grokbase.com/t/gg/akka user/1561gr0jgt/debug -信息-取消- akka -流- impl multistreamoutputprocessor女士-后- 5000

相关内容

  • 没有找到相关文章