【发布时间】:2014-05-01 16:37:45
【问题描述】:
我有以下服务器端播放代码,以便为 HTML5 EventSources 提供端点。
package controllers
import scala.util.matching
import play.api.mvc._
import play.api.libs.json.JsValue
import play.api.libs.iteratee.{Concurrent, Enumeratee}
import play.api.libs.EventSource
import play.api.libs.concurrent.Execution.Implicits._
object Application extends Controller {
/** Central hub for distributing chat messages */
val (eventOut, eventChannel) = Concurrent.broadcast[JsValue]
/** Enumeratee for filtering messages based on eventSpec */
def filter(eventSpec: String) = Enumeratee.filter[JsValue] {
json: JsValue => ("(?i)" + eventSpec).r.pattern.matcher((json \ "eventSpec").as[String]).matches
}
/** Enumeratee for detecting disconnect of SSE stream */
def connDeathWatch(addr: String): Enumeratee[JsValue, JsValue] =
Enumeratee.onIterateeDone{ () => println(addr + " - SSE disconnected") }
/** Controller action serving activity based on eventSpec */
def events = Action { req =>
println(req.remoteAddress + " - connected and subscribed for '" + eventSpec +"'")
Ok.feed(eventOut
&> filter(eventSpec)
&> Concurrent.buffer(50)
&> connDeathWatch(req.remoteAddress)
&> EventSource()
).as("text/event-stream").withHeaders(
"Access-Control-Allow-Origin" -> "*",
"Access-Control-Allow-Methods" -> "GET",
"Access-Control-Allow-Headers" -> "Content-Type"
)
}
}
我的路线是这样的
GET /events controllers.Application.events
当 Chrome 浏览器通过 EventSource 对象附加自身时,此服务器可以正常工作。由于 IE 不支持 EventSources,我使用的是 this polyfill 库。现在的问题:IE 正确订阅了事件,因为我看到了“已连接并已订阅”的日志输出,但只要将事件传递给此连接,它就会记录“SSE 已断开连接”。我在这里想念什么?一些http头?
【问题讨论】:
标签: html scala playframework-2.0 server-sent-events