如何从服务器向客户端发送消息? [英] How to send messages from server to client?

查看:298
本文介绍了如何从服务器向客户端发送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将实施类似于Facebook通知和本网站的内容(StackOverflow的通知,如果有人为我们的问题撰写评论/答案等,则会通知我们)。请注意,用户将使用我的应用程序作为网站而不是移动应用程序。

I am going to implement something similar to Facebook notification and this website (StackOverflow's notification which is notifying us if anyone write a comment/answer etc for our question). Please note users are going to use my application as a website not mobile application.

我遇到了以下 answer 获取结果,但我需要推送结果而不是fetch。

I came across following answer which fetch the results, but I need to push the results not fetch.

根据建议,我在我的实体类中创建了一个简单的方法,并将@PostPersist添加到它,但它没有根据这个答案我添加了persistence.xml文件来定义监听器但是在session.save(用户)之后,aftersave方法没有被触发。

Based on suggestions I have created a simple method in my entity class and added the @PostPersist to it but it has not worked so based on this answer I added the persistence.xml file to define the listeners but after session.save(user) the aftersave method does not get triggered.

User.java

@Entity
public class User{
  .....
  @PostPersist
    public void aftersave(){
        System.err.println("*****this is post persist method****");
    }
}

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->

<property name="hibernate.ejb.event.pre-insert"  value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.pre-update"  value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.pre-delete"  value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.post-insert" value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.post-update" value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.post-delete" value="my.hibernate.events.listeners.Listener" />

pom.xml

 <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.2.1.Final</version>
            <type>jar</type>
        </dependency>


推荐答案

听起来像 WebSockets 。它是Java EE 7的一部分,因此Glassfish应该是第一个支持它的AS之一。

Sounds like a task for WebSockets. It is part of Java EE 7 so the Glassfish should be one of the first AS that will support it.

为了拦截数据库访问,你可以使用 @PostUpdate @PostPersist 。以下是问题

For intercepting the DB access you can use @PostUpdate and @PostPersist. Here is related question.

有许多方法可以通过所谓的服务器推送来通知已连接的客户端:

There are many ways to do the so called Server Push for notifying the connected clients:

  • polling (the link you've provided in the question ("Are we there yet? Are we there yet? ..."))
  • long polling (smarter way of polling - long-lived HTTP technique using keepalive messages)
  • WebSockets (JSR 356)
  • piggy-backing
  • SPDY(wiki)
  • Server-Sent Events (related answer, wiki)

编辑:在Java世界中,有几个框架在其中实现服务器推送(反向ajax)盒子。如果您熟悉 GWT ,我建议 Errai 。另一种选择是 Atmospere 。 Atmospere的缺点是它需要使用您的Web应用程序在常规应用程序服务器旁边的独立运行过程。我在一年前玩它,所以从那时起可能已经改变了。

In the Java world, there are couple of frameworks where server push (reverse ajax) is implemented out-of-the box. If you are familiar with GWT, I would suggest Errai. Other alternative is the Atmospere. The downside of the Atmospere is the fact that it requires standalone running process next to your regular application server with your web app. I was playing with it a year ago so this may have been changed since then.

一般来说,很难为你提供具体的代码,因为它取决于您将选择的框架。我熟悉 Errai ,所以这里有一个例子:

In general, it is hard to provide you with a concrete piece of code, because it depends on the framework you will choose. I am familiar with Errai so here is an example in it:

服务器端:

@ApplicationScoped
public class TickerService {

  @Inject
  private Event<Tick> tickEvent;

  private void sendTick() {
    tickEvent.fire(new Tick());
  }
} 

客户端:

@EntryPoint
public class TickerClient {
  public void tickHappened(@Observes Tick tick) {

    // update the UI with the new data
  }
}

使用Errai的其他好处是在服务器和开箱即用的客户端上使用 CDI ,另一件好事就是使用封面下的网络套接字它受支持并退回到其他解决方案。

Other benefits of using the Errai is having the CDI on the server and on the client out-of-the-box, another thing that is nice is using the web-sockets under the covers if it is supported and falling back to other solutions otherwise.

无论您选择什么,它都应该适合您现有的基础架构和客户端UI框架。

这篇关于如何从服务器向客户端发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆