org.omnifaces.cdi.push.SocketChannelManager$ViewScope 无法转换为 com.sun.faces.push.WebsocketChannelManager$ViewScope

org.omnifaces.cdi.push.SocketChannelManager$ViewScope cannot be cast to com.sun.faces.push.WebsocketChannelManager$ViewScope

它是 my previous question 的后续,可能是由于 Mojarra 2.3.9 中的错误,我无法使 f:websocket 工作。

我想再试一次 o:socket

我的依赖项包括:

连接已打开,但是当我从 bean 发送消息时,没有任何反应。

@ViewScoped
@Named("ButtonController")
public class ButtonController implements Serializable {

    @Inject
    @Setter
    @Getter
    @Push(channel = "someChannel")
    private PushContext _someChannel;

    public void onPress(TaskButton button) {
        _someChannel.send("someEvent");
        _someChannel.send(new Object());
    }

}

<p:commandButton widgetVar="#{widget.id}"
                 value="#{widget.value}"
                 disabled="#{widget.running}"
                 actionListener="#{ButtonController.onPress(widget)}"
/>

<o:socket channel="someChannel" onmessage="socketListener" onopen="_onopen" scope="view"/>
<h:outputScript>
    function socketListener(message, channel, event) {
        console.log(message);
    }

    function _onopen() {
        console.log('_onopen')
    }
</h:outputScript>

这里

in com.sun.faces.push.WebsocketPushContext.java

@Override
public Set<Future<Void>> send(Object message) {
    return socketSessions.send(getChannelId(channel, sessionScope, viewScope), message);
}

getChannelId returns null,两个作用域总是空的。我改变了套接字的范围无济于事。

scope为view时,抛出此异常

java.lang.ClassCastException: org.omnifaces.cdi.push.SocketChannelManager$ViewScope cannot be cast to com.sun.faces.push.WebsocketChannelManager$ViewScope
    at com.sun.faces.push.WebsocketChannelManager.getViewScope(WebsocketChannelManager.java:233)
    at com.sun.faces.push.WebsocketPushContext.<init>(WebsocketPushContext.java:97)
    at com.sun.faces.cdi.WebsocketPushContextProducer.produce(WebsocketPushContextProducer.java:86)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jboss.weld.injection.StaticMethodInjectionPoint.invoke(StaticMethodInjectionPoint.java:88)
    at org.jboss.weld.injection.StaticMethodInjectionPoint.invoke(StaticMethodInjectionPoint.java:78)
    at org.jboss.weld.injection.producer.ProducerMethodProducer.produce(ProducerMethodProducer.java:100)
    at org.jboss.weld.injection.producer.AbstractMemberProducer.produce(AbstractMemberProducer.java:161)
    at org.jboss.weld.bean.AbstractProducerBean.create(AbstractProducerBean.java:180)
    at org.jboss.weld.context.unbound.DependentContextImpl.get(DependentContextImpl.java:70)
    at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:100)
    at org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50)
    at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:785)
    at org.jboss.weld.manager.BeanManagerImpl.getInjectableReference(BeanManagerImpl.java:885)
    at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:92)
    at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:358)
    at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:369)
    at org.jboss.weld.injection.producer.ResourceInjector.proceed(ResourceInjector.java:70)
    at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48)
    at org.jboss.weld.injection.producer.ResourceInjector.inject(ResourceInjector.java:72)
    at org.jboss.weld.injection.producer.BasicInjectionTarget.inject(BasicInjectionTarget.java:117)
    at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:159)
    at org.jboss.weld.util.bean.IsolatedForwardingBean.create(IsolatedForwardingBean.java:45)
    at com.sun.faces.application.view.ViewScopeContextManager.createBean(ViewScopeContextManager.java:145)
    at com.sun.faces.application.view.ViewScopeContext.get(ViewScopeContext.java:137)
    at org.jboss.weld.context.PassivatingContextWrapper$AbstractPassivatingContextWrapper.get(PassivatingContextWrapper.java:76)
    at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:100)
    at org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50)
    at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:785)
    at org.jboss.weld.el.AbstractWeldELResolver.lookup(AbstractWeldELResolver.java:107)
    at org.jboss.weld.el.AbstractWeldELResolver.getValue(AbstractWeldELResolver.java:90)
    at org.jboss.weld.environment.servlet.util.ForwardingELResolver.getValue(ForwardingELResolver.java:49)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:62)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:180)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:208)
    at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:94)
    at org.apache.el.parser.AstValue.getTarget(AstValue.java:70)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:228)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:107)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:148)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:96)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:839)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:870)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1418)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:201)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:670)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.omnifaces.filter.FacesExceptionFilter.doFilter(FacesExceptionFilter.java:97)
    at mycompany.exception.handler.MyFilter.doFilter(MyFilter.java:20)
    at org.omnifaces.filter.HttpFilter.doFilter(HttpFilter.java:108)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.omnifaces.filter.FacesExceptionFilter.doFilter(FacesExceptionFilter.java:97)
    at org.omnifaces.filter.HttpFilter.doFilter(HttpFilter.java:108)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:111)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.omnifaces.facesviews.FacesViewsForwardingFilter.filterExtensionLessToExtension(FacesViewsForwardingFilter.java:184)
    at org.omnifaces.facesviews.FacesViewsForwardingFilter.filterExtensionLess(FacesViewsForwardingFilter.java:141)
    at org.omnifaces.facesviews.FacesViewsForwardingFilter.filterExtensionLess(FacesViewsForwardingFilter.java:127)
    at org.omnifaces.facesviews.FacesViewsForwardingFilter.doFilter(FacesViewsForwardingFilter.java:88)
    at org.omnifaces.filter.HttpFilter.doFilter(HttpFilter.java:108)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:528)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:810)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

我不知道哪里会搞砸。任何帮助将不胜感激。

java.lang.ClassCastException: org.omnifaces.cdi.push.SocketChannelManager$ViewScope cannot be cast to com.sun.faces.push.WebsocketChannelManager$ViewScope

如果您在 bean 中将 javax.faces.push.Push instead of org.omnifaces.cdi.Push 作为 @Push 导入,就会发生这种情况。

仔细检查您的导入。在 JSF 2.3 环境中使用 OmniFaces <o:socket> 时,确保 @PushPushContext 是从 org.omnifaces.* 包而不是 javax.faces.* 包导入的,因为 @PushPushContext 是从 javax.faces.* 包导入的,因为 class 名字。