我将 EventSourceHQ 与 Heroku 一起使用。我没有使用现有的客户端,而是在 Scala 中重新编写客户端。正如Ruby 实现所示,这非常简单。
创建套接字成功,客户端 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;
})();
我可能哪里出错了?我该如何进一步调试呢?