java
  • spring
  • websocket
  • cxf
  • cxfrs
  • 2014-09-24 54 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"); 
    
    } 
    
    相關問題