java
  • spring
  • websocket
  • cxf
  • cxfrs
  • 2014-09-24 114 views 1 likes 
    1

    我试图使用websocket将一些监控消息推送到我的客户端jsf页面。但从过去的2天开始接近解决,但仍然无法将消息推送到jsf页面,虽然我已经创建了与web套接字的连接。CXF WebSocket配置

    这是我的javaScript代码。

    websocket = new WebSocket("ws://localhost:9000/jdbc/monitor"); 
    websocket.binaryType = 'arraybuffer'; 
    
    websocket.onopen = function(evt) { 
        onOpen(evt); 
    }; 
    
    websocket.onclose = function(evt) { 
        onClose(evt); 
    }; 
    
    websocket.onmessage = function(evt) { 
        onMessage(evt); 
    }; 
    
    websocket.onerror = function(evt) { 
        onError(evt); 
    }; 
    
    function startSocket() { 
    
        websocket.send("Started"); 
    
    } 
    
    function stopSocket() { 
    
        websocket.close(); 
    
    } 
    
    function onOpen(evt) { 
        console.log("Connection opened" + evt); 
    } 
    
    function onClose(evt) { 
        console.log("Connection closed" + evt); 
    } 
    
    function onMessage(evt) { 
        console.log("Recieved message" + evt); 
    } 
    
    function onError(evt) { 
        console.log("error occured" + evt); 
    } 
    
    function doSend(message) { 
        console.log("sending message..."); 
        websocket.send(reqmsg.value); 
    } 
    

    的CXF休息类

    @Path("/jdbc/") 
    @Service 
    public class JdbcMonitor { 
    
        private final static Logger LOG = LoggerFactory.getLogger(JdbcMonitor.class); 
        private final Set<OutputStream> monitor = new HashSet<OutputStream>(); 
    
        @GET 
        @Path("/monitor") 
        @Produces("text/*") 
        public StreamingOutput monitor() { 
    
         return new StreamingOutput() { 
    
          @Override 
          public void write(OutputStream wr) throws IOException, 
            WebApplicationException { 
           LOG.debug("Recieved a connection"); 
           monitor.add(wr); 
           wr.write(("Regsitered for monitor at" + new java.util.Date() 
             .toString()).getBytes()); 
           LOG.debug("Write completed"); 
    
          } 
         }; 
    
        } 
    
        public synchronized void sendData(final String message) { 
    
         LOG.debug("Triggered a websocket send"); 
         int i=0; 
         for (final Iterator<OutputStream> it = monitor.iterator();it.hasNext();) { 
    
          LOG.debug("Sending number"+ i++); 
          final OutputStream out = it.next(); 
          try { 
           out.write(message.getBytes()); 
          } catch (IOException e) { 
           try { 
            out.close(); 
            LOG.debug("Closing connection"); 
           } catch (IOException e1) { 
            LOG.debug("Closing connection"); 
           } 
           it.remove(); 
          } 
    
         } 
         LOG.debug("Websocket send completed"); 
    
        } 
    
    } 
    

    和CXF bean类

    <jaxrs:server id="kpWebSockServer" address="ws://localhost:9000/" > 
         <jaxrs:serviceBeans> 
          <ref bean="jdbcMonitor" /> 
         </jaxrs:serviceBeans> 
    </jaxrs:server> 
    

    我正在使用的代码是类似于CXF样品的提供。我可以连接到服务器。因为FireFox显示日志Connection opened[object Event],并且经过一段时间后我收到了关闭的连接日志,因为客户端 - 服务器之间没有通信。然而,日志显示jetty收到消息但未能将其转发给监视方法。因此我相信我错过了一些码头相关的配置,以便可以从码头服务器将消息委托给CXF(整个设置部署在tomcat7服务器中,不支持websocket)。有人能帮助指出缺失的链接。这里是码头相关的日志。

    2014-09-24 21:06:23,997 [qtp10414855-16 Selector0] DEBUG org.eclipse.jetty.io.nio - created [email protected]{l(/127.0.0.1:57569)<->r(/127.0.0.1:9000),s=0,open=true,ishut=false,oshut=false,rb=false,wb=false,w=true,i=0}-{[email protected],g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0} 
    2014-09-24 21:06:24,001 [qtp10414855-21] DEBUG org.eclipse.jetty.http.HttpParser - filled 468/468 
    2014-09-24 21:06:24,015 [qtp10414855-21 - /jdbc/monitor] DEBUG org.eclipse.jetty.server.Server - REQUEST /jdbc/monitor on [email protected],g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-5,l=9,c=0},r=1 
    2014-09-24 21:06:24,016 [qtp10414855-21 - /jdbc/monitor] DEBUG o.e.j.server.handler.ContextHandler - scope null||/jdbc/monitor @ o.e.j.s.h.ContextHandler{,null} 
    2014-09-24 21:06:24,016 [qtp10414855-21 - /jdbc/monitor] DEBUG o.e.j.server.handler.ContextHandler - context=||/jdbc/monitor @ o.e.j.s.h.ContextHandler{,null} 
    2014-09-24 21:06:24,047 [qtp10414855-21 - /jdbc/monitor] DEBUG o.e.j.websocket.WebSocketFactory - extensions=[] 
    2014-09-24 21:06:24,094 [qtp10414855-21 - /jdbc/monitor] INFO o.a.c.t.w.jetty.JettyWebSocket - onOpen([email protected] l(127.0.0.1:9000)<->r(127.0.0.1:57569))) 
    2014-09-24 21:06:24,095 [qtp10414855-21 - /jdbc/monitor] DEBUG o.e.j.websocket.WebSocketFactory - Websocket upgrade /jdbc/monitor 13 null WebSocketServletConnectionRFC6455 [email protected] state=START buffer=null [email protected] closed=false buffer=-1 
    2014-09-24 21:06:24,095 [qtp10414855-21 - /jdbc/monitor] DEBUG org.eclipse.jetty.server.Server - RESPONSE /jdbc/monitor 101 handled=true 
    2014-09-24 21:06:24,096 [qtp10414855-21] DEBUG o.e.j.server.AsyncHttpConnection - Enabled read interest [email protected]{l(/127.0.0.1:57569)<->r(/127.0.0.1:9000),s=1,open=true,ishut=false,oshut=false,rb=false,wb=false,w=true,i=0}-{[email protected],g=HttpGenerator{s=4,h=0,b=-1,c=-1},p=HttpParser{s=0,l=9,c=0},r=1} 
    2014-09-24 21:06:24,130 [qtp10414855-21] DEBUG org.eclipse.jetty.io.nio - WebSocketServletConnectionRFC6455 [email protected] state=START buffer=null [email protected] closed=false buffer=-1 replaced [email protected],g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=-3},r=1 
    
    +0

    你能请添加行家/ CXF配置,你必须支持的WebSockets与码头/ CXF的细节? 在此先感谢 – 2016-08-25 11:59:15

    +0

    也你知道如何添加标题的发送消息,在StreamingOutput? – 2016-08-25 12:00:54

    回答

    1

    终于找到了我的问题。它是JavaScript代码的问题。在发送方法我应该通过URI以http方式键入下面的代码修复了这个问题

    function startSocket() { 
    
        websocket.send("Get /jdbc/monitor"); 
    
    } 
    
    相关问题