我在Heroku中使用EventSourceHQ。我沒有使用現有的客戶端,而是在Scala中重寫客戶端。這很簡單,如Rubyimplementation所示。EventSourceHQ發送事件返回OK,但什麼都不做
創建一個套接字是成功的,客戶端JavaScript似乎很滿意所產生的socket_id。但是,當我在服務器eventsourcehq.com/event
上觸發事件返回HTTP 200
與正文爲{ }
。此外,沒有事件處理程序在瀏覽器中執行。也就是說,服務器向eventsourcehq.com發送一個事件,但該事件不會轉發給瀏覽器中的客戶端。
的我是如何發佈的服務器(使用調度)來eventsourcehq的肉:
val (key, secret, serviceURL) =
(properties("ESHQ_KEY"), properties("ESHQ_SECRET"), properties("ESHQ_URL"))
def createSocket(channel: String) = post("/socket", Map("channel" -> channel))
def publish(data: String, channel: String) = post("/event",
Map("channel" -> channel, "data" -> data))
private def post(path: String, params: Map[String, String]): ActionResult = {
val request = url(s"$serviceURL$path") << params << credentials
Await.result(Http(request).either, 5.seconds).fold(
{t => InternalServerError(s"Failed to post $request: ${t.getMessage}")},
{r => Ok(r.getResponseBody)}
)
}
private def credentials = {
val time = (System.currentTimeMillis/1000).toString
Map("key" -> key, "timestamp" -> time, "token" -> token(key, secret, time))
}
private def token(strings: String*) = {
val md = java.security.MessageDigest.getInstance("SHA-1")
md.digest(strings.mkString(":").getBytes("UTF-8")).map("%02x".format(_)).mkString
}
由於在Ruby客戶端,無論是createSocket
(open
)和publish
(send
)方法委託給同http發佈邏輯。
在客戶端:
var serverEvents = (function() {
var eshq = new ESHQ("forkpin");
// callback called when the connection is made
eshq.onopen = function(e) {
console.log("Open event", e);
};
// called when a new message with no specific type has been received
eshq.onmessage = function(e) {
console.log("Message type: %s, message data: %s", e.type, e.data);
};
// callback called on error
eshq.onerror = function(e) {
console.log("Error event", e);
};
return eshq;
})();
如果可能我會錯誤?我怎樣才能進一步調試呢?
這是比回答更多的評論。我知道你需要更多的聲譽... – rene
是的,希望我可以把它變成一個很好的答案,一旦我有一些關於客戶端代碼的信息:) – Biilmann
感謝您的關注。 JS補充說。如果這歸結爲缺乏js排印,我會否認自己是糕點。 – Synesso