我正试图获得" Counter" Primefaces推动样品工作。它实际上正在工作,但我必须刷新页面一次才能打开推送端点。如何在第一页加载时打开它?
我在Tomcat 8.0.24上运行它。
以下是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>
终点:
@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!");
}
}
Managed 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));
}
}
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>
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>
运行此代码时,当我第一次加载页面并单击按钮时没有输出,该值在服务器端递增,但显示的值不会因端点未打开而发生变化。
然后,当我刷新页面时,端点打开,显示计数器的正确值,当我单击按钮时,该值会递增并按预期显示。
我非常感谢你提供任何帮助。谢谢你的帮助。
答案 0 :(得分:0)
将应用程序服务器从Tomcat 8.0.24更改为Tomcat 7.0.63解决了这个问题!
我对这个解决方案没问题,因为我绝对不需要Tomcat 8。
感谢@Kukeltje,我注意到Tomcat 7和Tomcat 8在首页加载时在网络级别发生了一些差异。我认为这个问题可能来自大气 - 运行时,我会进一步研究它以满足我的好奇心:)