0
以下是document推薦的工作。如何讓websocket在play和Concurrent.broadcast上工作
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 the message to stdout and send response back to client
val in = Iteratee.foreach[String] {
msg => println(msg)
// the Enumerator returned by Concurrent.broadcast subscribes to the channel and will
// receive the pushed messages
channel push("I received your message: " + msg)
}
(in,out)
}
但是它不工作,如果我更改爲:
def socket = WebSocket.using[String] { request =>
val (out, channel) = Concurrent.broadcast[String]
val in=Iteratee.ignore[String]
channel push("Hello World")
(in,out)
}
我會很感激,如果你能幫助我理解爲什麼它不與新辦法的工作。
感謝
詹姆斯
更新:
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)
}
我想通過「不工作」你意味着什麼都不會推回到客戶端。我只是在猜測,但這可能是因爲你在與客戶端連接完成之前推送到頻道,所以他們沒有收到特定的消息。 – 2015-04-01 01:28:39
「不工作」是什麼意思? – Ryan 2015-04-01 01:53:28
是的。這意味着客戶端無法收到「hello world」消息。 – user1588745 2015-04-01 01:55:11