简体   繁体   English

Primefaces推动怪异行为

[英]Primefaces push weird behaviour

I am trying to get the "Counter" Primefaces Push sample working. 我正在尝试使“ Counter” Primefaces Push示例工作。 It's actually working but i have to refresh the page once to get the push endpoint open. 它实际上在工作,但是我必须刷新页面一次才能打开推送端点。 How can I have it open at the first page load ? 如何在第一页加载时打开它?

I am running this on Tomcat 8.0.24. 我正在Tomcat 8.0.24上运行它。

Here are the dependencies in the pom.xml : 这是pom.xml中的依赖项:

<dependencies>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>servlet-api</artifactId>
        <version>6.0.44</version>
    </dependency>
    <dependency>
        <groupId>org.apache.myfaces.core</groupId>
        <artifactId>myfaces-api</artifactId>
        <version>2.2.8</version>
    </dependency>
    <dependency>
        <groupId>org.apache.myfaces.core</groupId>
        <artifactId>myfaces-impl</artifactId>
        <version>2.2.8</version>
    </dependency>
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>5.2</version>
    </dependency>
    <dependency>
        <groupId>org.atmosphere</groupId>
        <artifactId>atmosphere-runtime</artifactId>
        <version>2.3.3</version>
    </dependency>
</dependencies>

The EndPoint: 终点:

@PushEndpoint("/counter")
public class CounterResource {

  @OnMessage(encoders = { JSONEncoder.class })
  public String onMessage(String count) {
    return count;
  }

  @OnOpen
  public void onOpen(RemoteEndpoint r, EventBus e) {
    System.out.println("CounterResource:onOpen PushEndpoint open!");
  }

  @OnClose
  public void onClose(RemoteEndpoint r, EventBus e) {
    System.out.println("CounterResource:onClose PushEndpoint closed!");
  }
}

The Managed Bean: 托管Bean:

@ManagedBean
@ApplicationScoped
public class GlobalCounterView implements Serializable {

  private volatile int count;

  public int getCount() {
    return count;
  }

  public void setCount(int count) {
    this.count = count;
  }

  public void increment() {
    count++;

    EventBus eventBus = EventBusFactory.getDefault().eventBus();
    eventBus.publish("/counter", String.valueOf(count));
  }
}

The counter.xhtml page: counter.xhtml页面:

<h:body>
<h:form id="form">
    <h:outputText id="out" value="#{globalCounterView.count}"
        styleClass="ui-widget display" />

    <p:commandButton value="Click"
        actionListener="#{globalCounterView.increment}" />
</h:form>

<p:socket onMessage="handleMessage" channel="/counter" />

<script type="text/javascript">
    /* ugly workaround here
    window.onload = function() {
        if (!localStorage.justOnce) {
            localStorage.setItem("justOnce", "true");
            window.location.reload();
        }
    }*/

    function handleMessage(data) {
        $('.display').html(data);
    }
</script>

The web.xml: web.xml:

<!-- JSF -->
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

<!-- Primefaces Push -->
<servlet>
    <servlet-name>Push Servlet</servlet-name>
    <servlet-class>org.primefaces.push.PushServlet</servlet-class>
    <init-param>
        <param-name>org.atmosphere.cpr.broadcasterCacheClass</param-name>
        <param-value>org.atmosphere.cache.UUIDBroadcasterCache</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
</servlet>

<servlet-mapping>
    <servlet-name>Push Servlet</servlet-name>
    <url-pattern>/primepush/*</url-pattern>
</servlet-mapping>

When running this code I have no output when I first load the page and click the button, the value is incremented on the server side but the displayed value doesn't change since the endpoint is not open. 在运行此代码时,当我第一次加载页面并单击按钮时,我没有输出,该值在服务器端递增,但由于未打开端点,因此显示的值未更改。

Then, when I refresh the page, the endpoint opens, the correct value of the counter is displayed and when I click the button, the value is incremented and displayed as intended. 然后,当我刷新页面时,端点将打开,显示正确的计数器值,并且当我单击按钮时,该值将增加并按预期显示。

I would greatly appreciate any help on this. 我将不胜感激对此的任何帮助。 Thank you for your assistance. 谢谢您的帮助。

Changing the application server from Tomcat 8.0.24 to Tomcat 7.0.63 solved the problem ! 将应用程序服务器从Tomcat 8.0.24更改为Tomcat 7.0.63可以解决此问题!

I'm OK with that solution since I don't need Tomcat 8 absolutely. 我可以使用该解决方案,因为我绝对不需要Tomcat 8。

And thanks to @Kukeltje I noticed some difference between Tomcat 7 and Tomcat 8 happening at the network level at first page load. 感谢@Kukeltje,我注意到在第一次加载页面时,在网络级别发生了Tomcat 7和Tomcat 8之间的一些差异。 I think the problem might come from atmosphere-runtime and I'll look further into that to satisfy my curiosity :) 我认为问题可能出在大气运行时,为了满足我的好奇心,我将作进一步调查:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM