Akka-http:连接到localhost上的websocket

时间:2017-12-17 20:21:37

标签: scala websocket akka-stream akka-http

我正在尝试通过localhost上的websocket连接到某个服务器。当我尝试在JS中执行

ws = new WebSocket('ws://localhost:8137');

成功了。但是,当我使用akka-http和akka-streams时,我得到"连接失败"错误。

object Transmitter {
    implicit val system: ActorSystem = ActorSystem()
    implicit val materializer: ActorMaterializer = ActorMaterializer()

    import system.dispatcher

    object Rec extends Actor {
        override def receive: Receive = {
            case TextMessage.Strict(msg) =>
                Log.info("Recevied signal " + msg)
        }
    }

    //  val host = "ws://echo.websocket.org"
    val host = "ws://localhost:8137"

    val sink: Sink[Message, NotUsed] = Sink.actorRef[Message](system.actorOf(Props(Rec)), PoisonPill)


    val source: Source[Message, NotUsed] = Source(List("test1", "test2") map (TextMessage(_)))


    val flow: Flow[Message, Message, Future[WebSocketUpgradeResponse]] =
        Http().webSocketClientFlow(WebSocketRequest(host))

    val (upgradeResponse, closed) =
        source
        .viaMat(flow)(Keep.right) // keep the materialized Future[WebSocketUpgradeResponse]
        .toMat(sink)(Keep.both) // also keep the Future[Done]
        .run()

    val connected: Future[Done.type] = upgradeResponse.flatMap { upgrade =>
        if (upgrade.response.status == StatusCodes.SwitchingProtocols) {
            Future.successful(Done)
        } else {
            Future.failed(new Exception(s"Connection failed: ${upgrade.response.status}")
        }
    }

    def test(): Unit = {
        connected.onComplete(Log.info)
    }
}

使用ws://echo.websocket.org完全正常。

我认为附加我服务器的代码是没有理由的,因为它适用于JavaScript客户端,问题只与连接有关,但是如果你想查看它我可能会显示它。

我做错了什么?

2 个答案:

答案 0 :(得分:1)

我使用来自akka documentation的websocket服务器测试了您的客户端实现, 我没有得到任何连接错误。您的websocket客户端连接成功。这就是为什么我猜测问题与您的服务器实现有关。

object WebSocketServer extends App {
  implicit val system = ActorSystem()
  implicit val materializer = ActorMaterializer()
  import Directives._

  val greeterWebSocketService = Flow[Message].collect {
    case tm: TextMessage => TextMessage(Source.single("Hello ") ++ tm.textStream)
  }

  val route =
    get {
      handleWebSocketMessages(greeterWebSocketService)
    }

  val bindingFuture = Http().bindAndHandle(route, "localhost", 8137)

  println(s"Server online at http://localhost:8137/\nPress RETURN to stop...")
  StdIn.readLine()

  import system.dispatcher // for the future transformations
  bindingFuture
    .flatMap(_.unbind()) // trigger unbinding from the port
    .onComplete(_ => system.terminate()) // and shutdown when done
}

顺便说一句,我注意到你的演员的接收方法没有涵盖所有可能的消息。根据{{​​3}}, 每条消息,甚至非常小,最终都可以Streamed。如果要打印所有文本消息,演员的更好实现将是:

object Rec extends Actor {
  override def receive: Receive = {
    case TextMessage.Strict(text)             ⇒ println(s"Received signal $text")
    case TextMessage.Streamed(textStream)     ⇒ textStream.runFold("")(_ + _).foreach(msg => println(s"Received streamed signal: $msg"))
  }
}

请在that akka issue找到一个有效的项目。

答案 1 :(得分:0)

我找到了解决方案:我使用的服务器在IPv6上运行(如:: 1),但akka-http将localhost视为127.0.0.1并忽略:: 1。我不得不重写服务器以强制它使用IPv4并且它有效。