Firebase 服务器发送的事件 - 如何构建 Java/JavaScript 客户端 [英] Firebase Server Sent Events - How to build a Java / JavaScript client
问题描述
我是 Firebase 的新手,正在构建原型来测试是否适合我们的需求.我已经启动并运行了一个聊天示例,到目前为止一切顺利.
I am new to Firebase and building a prototype to test if suitable for our needs. I've got a chat example up and running, so far so good.
接下来是需要监听变化.我尝试使用 Java 和 JavaScript 连接到支持服务器发送的事件的 REST API,但我无法使其工作.
Next is the need to listen for changes. I have tried connecting to the REST API supporting server sent events using both Java and JavaScript, but I can not make it work.
在 Java 中,我编写了以下代码:
In Java I have written the following code:
public class FirebaseApplication {
@Test
public void test() throws Exception{
Client client = ClientBuilder.newBuilder()
.register(SseFeature.class).build();
WebTarget webTarget = client.target(new URI(
"http://incandescent-torch-xxxx.firebaseio.com/logs.json"));
EventSource eventSource = new EventSource(webTarget) {
@Override
public void onEvent(InboundEvent inboundEvent) {
System.out.println("Data " + inboundEvent.readData());
}
};
Thread.sleep(20000);
System.out.println("Exit");
eventSource.close();
}
}
然而,即使我与小程序的执行并行,我也没有收到任何事件.
I do however not receive any events even though I am in parallel with the execution of the little program.
接下来我尝试使用 JavaScript 客户端,但结果相同.我从来没有收到任何事件.
Next I tried using a JavaScript client, but with the same result. I never receive any events.
var source = new EventSource(
"http://incandescent-torch-xxxx.firebaseio.com/logs.json");
source.addEventListener('message', function(e) {
console.log(e.data);
}, false);
source.addEventListener('open', function(e) {
console.log("open");
// Connection was opened.
}, false);
source.addEventListener('error', function(e) {
if (e.readyState == EventSource.CLOSED) {
console.log("close");
// Connection was closed.
}else{
console.log("error");
console.log(e);
}
}, false);
有人知道我做错了什么吗?
Does anyone having a clue of what I am doing wrong ?
推荐答案
为了接收事件,您需要监听 'Firebase' 提供的事件,这些事件包括 put、patch、keep-alive 等......我已经尝试过它有效.
In order to receive events, you need to listen into 'Firebase' provided events which are put, patch, keep-alive etc... I have tried and it works.
您可以参考以下代码片段,
You can refer following code snippet,
if (typeof (EventSource) !== "undefined") {
var source = new EventSource("https://xxxxxxxxx.firebaseio.com/TestNode.json");
source.onmessage = function (event) {
document.getElementById("result").innerHTML += event.data + "<br>";
};
source.addEventListener("message", function (e) {
console.log(e.data);
}, false);
source.addEventListener("open", function (e) {
console.log("Connection was opened.");
}, false);
source.addEventListener("error", function (e) {
console.log("Error - connection was lost.");
}, false);
//magic goes here!
source.addEventListener("patch", function (e) {
console.log("Patch UP - " + e.data);
}, false);
//And here!
source.addEventListener("put", function (e) {
console.log("Put UP - " + e.data);
}, false);
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events...";
}
参考以下文档,https://firebase.google.com/docs/database/rest/retrieve-数据
https://firebase.google.com/docs/reference/rest/数据库/
这篇关于Firebase 服务器发送的事件 - 如何构建 Java/JavaScript 客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!