2012-02-02 76 views
1

握手正確完成,服務器可以解碼來自客戶端的數據,但客戶端在我嘗試向其發送數據時關閉連接。Java:WebSocket服務器不會正確地向客戶端發送數據

我一直在使用http://websocket.org/echo.html作爲客戶端w。 Chrome的最新版本爲&。

這裏的數據幀我想送:負責將數據發送到客戶端

129 10000001 
4 100 
116 1110100 
101 1100101 
115 1110011 
116 1110100 
------- 
fin:true 
opcode:1 
len:4 
masked:false 
masks:[0, 0, 0, 0] 
payload:test 
?♦test 

http://tools.ietf.org/html/rfc6455#section-5 
     0     1     2     3 
     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 
    +-+-+-+-+-------+-+-------------+-------------------------------+ 
    |F|R|R|R| opcode|M| Payload len | Extended payload length | 
    |I|S|S|S| (4) |A|  (7)  |    (16/64)   | 
    |N|V|V|V|  |S|    | (if payload len==126/127) | 
    | |1|2|3|  |K|    |        | 
    +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - + 
    |  Extended payload length continued, if payload len == 127 | 
    + - - - - - - - - - - - - - - - +-------------------------------+ 
    |        |Masking-key, if MASK set to 1 | 
    +-------------------------------+-------------------------------+ 
    | Masking-key (continued)  |   Payload Data   | 
    +-------------------------------- - - - - - - - - - - - - - - - + 
    :      Payload Data continued ...    : 
    + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + 
    |      Payload Data continued ...    | 
    +---------------------------------------------------------------+ 


*/ 

而服務器端的方法:

public void sendData(Socket socket, byte[] dataBytes){ 
     System.out.println(java.util.Arrays.toString(dataBytes)); 
     //[-127, 4, 116, 101, 115, 116] 
     for(byte b:dataBytes) System.out.println(Integer.toString((int)0xff&b,2)); 
     /* 
     10000001 
     100 
     1110100 
     1100101 
     1110011 
     1110100   
     */ 

     try{ 

       InputStream data = new ByteArrayInputStream(dataBytes); 
       OutputStream out = socket.getOutputStream(); 
       //tested with ByteArrayOutputStream and written data == dataBytes 


       //out.write((byte)0x00); //tried with and without this 
       if (data != null) 
       { 
       // tried also out.write(dataBytes) intstead of this 
         byte[] buff = new byte[2048]; 
         while (true) 
         { 
             int read = data.read(buff, 0, 2048); 
             if (read <= 0) 
               break; 
             out.write(buff, 0, read); 
           } 
         } 
       //out.write(-1);    
       //out.write((byte)0xFF);  
       out.flush(); 

       //out.close(); 
       if (data != null) 
         data.close(); 


     }catch(Exception e){ 
       e.printStackTrace(); 
       sockets.remove(socket); 
     } 

} 

回答

0

一些問題:

  • 在從服務器發送之前是否等待連接完全打開?
  • 你能捕捉使用wireshark的流,看看線上究竟是什麼?
  • 在Chrome的Javascript控制檯中,您是否看到任何與WebSocket相關的錯誤?
  • 在你的JavaScript websocket對象的onclose處理程序中,你可以console.log從事件的代碼和原因的值?

像這樣:

ws.onclose = function (e) { 
    console.log("closed - code " + e.code + ", reason " + reason); 
} 
+0

1.是2.試圖將數據寫入ByteArrayOutputStream中並檢出,然後我嘗試wireshark 3.「無法識別的幀操作碼:15」4.「關閉 - 代碼1006,原因」 – Seppo420 2012-02-02 15:55:47

+0

Can'弄清楚如何用wireshark過濾相關的軟件包... – Seppo420 2012-02-02 16:26:41

+0

明白了。我完全不知道什麼是錯的,或者我做了什麼來解決它,但它現在起作用。 http://mlkshk.com/r/4ILY – Seppo420 2012-02-02 16:57:03

-1

您的問題(可能)使用舊的協議。使用較新的一個...

@see

http://web-sockets.org

有工作源客戶端和服務器(在Java中)。

相關問題