推送Java Web应用程序的通知

问题描述:

目前我正在使用一个使用Spring 3.1和Hibernate 4的网络应用程序。

Currently I am working on a web app which uses Spring 3.1 and Hibernate 4.

根据要求,我想实现像Facebook那样的推送通知。一个JSP页面。如果您有任何建议,请同时列出兼容的浏览器及其版本。

As per the requirement, I want to implement push notifications like Facebook does, on a JSP page. If you have any suggestions, then please also list the compatible browsers with their versions.

如果您可以升级或正在使用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>))

希望这会有所帮助