我尝试使用 JSF 2.3.3(Glassfish 实现)测试新的 WebSocket 功能。我使用 Tomcat 9.0.1 作为 Web 服务器并遵循本指南(https://javaserverfaces.github.io/whats-new-in-jsf23.html)
我创建了一个托管 bean:
@Named
@ApplicationScoped
public class Controller {
@Inject @Push
private PushContext cityChannel;
public void send() {
cityChannel.send("test");
}
}
更新 index.xhtml:
<f:websocket channel="cityChannel"
onmessage="function(message){alert(message)}" />
并更新了 web.xml:
<context-param>
<param-name>javax.faces.ENABLE_CDI_RESOLVER_CHAIN</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.ENABLE_WEBSOCKET_ENDPOINT</param-name>
<param-value>true</param-value>
</context-param>
不幸的是,Tomcat 无法加载应用程序并出现错误:
SEVERE [main] com.sun.faces.config.ConfigureListener.contextInitialized Critical error during deployment:
javax.websocket.DeploymentException: Multiple Endpoints may not be deployed to the same path [/javax.faces.push/{channel}] : existing endpoint was [class com.sun.faces.push.WebsocketEndpoint] and new endpoint is [class com.sun.faces.push.WebsocketEndpoint]
at org.apache.tomcat.websocket.server.WsServerContainer.addEndpoint(WsServerContainer.java:169)
如果我删除上下文参数 javax.faces.ENABLE_WEBSOCKET_ENDPOINT 则 JSF 运行时会在我点击 index.xhtml 时引发错误:
Caused by: java.lang.IllegalStateException: f:websocket endpoint is not enabled. You need to set web.xml context param 'javax.faces.ENABLE_WEBSOCKET_ENDPOINT' with value 'true'.
请指教。