更新时间:2022-12-21 11:12:08
如果您可以升级到或正在使用 JDK 7,我建议您使用 Vert.x Vertx.io ,在客户端使用 Sockjs.Vert.x 有一个完整的 sockjs 服务器实现,我会尝试提出一种实现方法,其余的请看文档
If you can upgrade to or are using JDK 7 I suggest using Vert.x Vertx.io , use Sockjs on the client side. Vert.x has a complete sockjs server implementation, I ll try to suggest a way to implement this, for the rest please look at the Docs
服务器实现可能是这样的
The server implementation could be like this
Vertx vertx = Vertx.newVertx();
EventBus eventBus = vertx.eventBus()
HttpServer server = vertx.createHttpServer();
JsonArray permitted = new JsonArray();
permitted.add(new JsonObject());
SockJSServer sockJSServer = new DefaultSockJSServer(vertx, server);
sockJSServer.bridge(new JsonObject().putString("prefix", "/pusher"), permitted, permitted);
server.listen(<some port>);
在客户端注册一个像这样加载文档的处理程序
On the client side register a handler like so on document load
function () {
if (!eb) {
eb = new vertx.EventBus("http://<your_host>:<your_port>/pusher");
eb.onopen = function() {
console.log("connected")
};
eb.onclose = function() {
console.log("Not connected");
eb = null;
};
}
}
然后您可以将处理程序注册到任何地址 - 这里的地址可以是任何地址,假设它是AwesomeNotifications"
You can then register a handler to any address - address here can be anything , assume it is "AwesomeNotifications"
function subscribe(address) {
if (eb) {
eb.registerHandler(address, function(msg, replyTo) {
console.log("Reply recieved")
});
}
}
完成所有设置后,您现在可以使用我们之前创建的事件总线将任何数据从服务器推送到此地址
Once you have this all set up , you can now push any data from the server to this address using the event bus we created earlier
eventBus.publish("AwesomeNotifications", new JsonObject(<some hashmap to serialize>))
希望能帮到你