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

时间:2015-07-23 06:09:20

标签: websocket akka akka-http

我试图在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

1 个答案:

答案 0 :(得分:0)

仔细查看该错误消息......它来自我不希望的来源,一些&#34; MultiStreamOutputProcessor&#34;当我只期望处理单个流时。

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

所以不是它而是#34;只在某些时候工作,&#34;也许它是&#34;大部分时间都在工作但是无法处理你在流程中要求的所有输入,并且你留下了不可选择的流,必须先死掉。

看看你是否可以a)确保你抓住溪流,这样你就不会有落伍者,b)绑定调整超时,以及c)检测这样的事件和取消处理下游

https://groups.google.com/forum/#!topic/akka-user/x-tARRaJ0LQ

akka {    流{      物化者{        subscription-timeout {          超时= 30秒        }      }    } }

http://grokbase.com/t/gg/akka-user/1561gr0jgt/debug-message-cancelling-akka-stream-impl-multistreamoutputprocessor-after-5000-ms