scala - how to get websocket working with play and Concurrent.broadcast -
here document recommends , work.
import play.api.mvc._ import play.api.libs.iteratee._ import play.api.libs.concurrent.execution.implicits.defaultcontext def socket = websocket.using[string] { request => // concurrent.broadcast returns (enumerator, concurrent.channel) val (out, channel) = concurrent.broadcast[string] // log message stdout , send response client val in = iteratee.foreach[string] { msg => println(msg) // enumerator returned concurrent.broadcast subscribes channel , // receive pushed messages channel push("i received message: " + msg) } (in,out) } however not working if changed to:
def socket = websocket.using[string] { request => val (out, channel) = concurrent.broadcast[string] val in=iteratee.ignore[string] channel push("hello world") (in,out) } i'd appreciate if can me understand why not working new approach.
thanks
james
update:
class servicehandler extends actor { import tcp._ val (enumerator, channel) = concurrent.broadcast[string] val system=actordict.system def receive = { case subscribedata() =>{ sender ! enumerator } case received(data) => { val dst = data.decodestring("utf-8") val va=dst.substring(dst.lastindexof(',') + 1).trim() println(va) channel.push(va) } case peerclosed => context stop self } } def ws = websocket.using[string] { request => val in=iteratee.ignore[string] val datahandler = akka.system.actorof(props[servicehandler]) val out= await.result((datahandler ? subscribedata()), 5 seconds).asinstanceof[enumerator[string]] (in,out) }
you not receive "hello world" in client side because you're pushing channel before connection established client.
if don't need receive client ( why you're doing val in=iteratee.ignore[string] ) you'd better go server sent events:
ok.chunked(out &> eventsource()).as("text/event-stream")
Comments
Post a Comment