Akka HTTP WebSocket client equivalent of this node.js -
i have user documentation expresses how use websocket node snippet:
var socket = io(“host:port”); socket.on('request-server', function() { socket.emit('server-type', 'red') });
what equivalent client code in akka http?
i have derived following example in akka documentation. isn't quite i'd write, because
- i think need connect , wait
request-server
event before sending events & don't know how that - i don't know how format
textmessages
insource
equivalent `socket.emit('server-type', 'red').
it prints "closed"
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"))
what akka client equivalent given socket.io code?
your connection getting closed after sending message "outgoing".
check out half-closed websockets here http://doc.akka.io/docs/akka-http/10.0.0/scala/http/client-side/websocket-support.html#half-closed-websockets
Comments
Post a Comment