0
我有表達如何使用的WebSocket與此節點片斷一些用戶文檔:阿卡HTTP的WebSocket的這個客戶端等效的node.js
var socket = io(「HOST:PORT」);
socket.on('request-server', function() {
socket.emit('server-type', 'red')
});
就相當於客戶端代碼是什麼在阿卡HTTP?
我從Akka文檔中的例子推導出以下內容。這是不是很希望我寫的,因爲
- 我想我需要連接和發送任何事件之前等待
request-server
事件&我不知道該怎麼做 - 我不知道如何格式化
Source
中的TextMessages
以等效於`socket.emit('server-type','red')。
只打印 「封閉」
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
import system.dispatcher
val incoming: Sink[Message, Future[Done]] = Sink.foreach[Message] {
case message: TextMessage.Strict => println(message.text)
case z => println(z)
}
val outgoing = Source(List(TextMessage("'server-type': 'red'")))
val webSocketFlow = Http().webSocketClientFlow(
WebSocketRequest("ws://localhost:3000/socket.io"))
val (upgradeResponse, closed) =
outgoing
.viaMat(webSocketFlow)(Keep.right)
.toMat(incoming)(Keep.both)
.run()
val connected = upgradeResponse.flatMap { upgrade =>
if (upgrade.response.status == StatusCodes.SwitchingProtocols) {
Future.successful(Done)
} else {
throw new RuntimeException(s"Connection failed: ${upgrade.response.status}")
}
}
connected.onComplete(println)
closed.foreach(_ => println("closed"))
什麼是阿卡客戶端相當於給socket.io代碼?