2016-02-12 240 views
1

當從客戶端使用此LIB的sendBinary我得到了在服務器上的所有0;(的Java的WebSocket客戶端的lib問題:NV-的WebSocket客戶端sendBinary

此頁面要求我增加更多的描述,但該代碼波紋管簡單和直接的,應該是自我解釋的......

客戶端代碼:

private WebSocket connect() throws IOException, WebSocketException { 
    return new WebSocketFactory() 
      .setConnectionTimeout(5000) 
      .createSocket("ws://localhost:8080/testwsapp2/endpoint") 
      .addListener(new WebSocketAdapter() { 
       public void onBinaryMessage(WebSocket websocket, byte[] message) { 

        String strmsg = new String(message, StandardCharsets.US_ASCII); 
        System.out.println("message from server: " + strmsg); 

        //echo back 
        try { 
         strmsg = "echo: " + strmsg; 
         System.out.println("now echo back with \"" + strmsg + "\""); 

         byte[] bytemsg = strmsg.getBytes("US-ASCII"); 
         System.out.println("echo message length = " + bytemsg.length); 

         String a2sview = Arrays.toString(bytemsg); 
         System.out.println("echo message a2sview: " + a2sview); 

         websocket.sendBinary(bytemsg); 
        } catch (Exception ex) { 
         System.out.println(ex.getMessage()); 
        } 

       } 
      }) 
      .addExtension(WebSocketExtension.PERMESSAGE_DEFLATE) 
      .connect(); 
} 

服務器端代碼:

@OnOpen 
public void on_open(Session session) { 
    try { 
     byte[] bytemsg = ("hello client").getBytes("US-ASCII"); 
     session.getBasicRemote().sendBinary(ByteBuffer.wrap(bytemsg)); 
    } catch (Exception ex) { 
     System.out.println(ex.getMessage()); 
    } 
} 

@OnMessage 
public void on_message(Session session, ByteBuffer message) { 

    byte[] bytemsg = new byte[message.remaining()]; 
    System.out.println("client message length = " + bytemsg.length); 

    String a2sview = Arrays.toString(bytemsg); 
    System.out.println("client message a2sview: " + a2sview); 

    String strmsg = new String(bytemsg, StandardCharsets.US_ASCII); 
    System.out.println("client message: " + strmsg); 

} 

客戶端輸出:

message from server: hello client 
now echo back with "echo: hello client" 
echo message length = 18 
echo message a2sview: [101, 99, 104, 111, 58, 32, 104, 101, 108, 108, 111, 32, 99, 108, 105, 101, 110, 116] 

服務器輸出

Info: client message length = 18 
Info: client message a2sview: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 
Info: client message: 

非常感謝您的幫助。

+0

我GIT克隆對NV-WebSocket的客戶端的源代碼和非常懷疑操作 「INT B =(有效載荷[I]^maskingKey [則i%4])& 0xFF;」 在WebSocketOutputStream.java的136線。任何人都可以幫助和檢查? – howToDeleteMyAccount

+0

我收到了Takahiko Kawasaki親自給出的測試代碼的電子郵件回覆,當使用ws://echo.websocket.org進行測試時,它們都很好。所以服務器端的代碼一定有問題。附: server = glassfish4.1.1,platform = centos7 – howToDeleteMyAccount

回答

0

在服務器端on_message方法,你有你傳遞bytemsgArray.toString之前的message內容複製到bytemsg

byte[] bytemsg = new byte[message.remaining()]; 
System.out.println("client message length = " + bytemsg.length); 

// !!! Copy the content of 'message' to 'bytemsg' here !!! 

String a2sview = Arrays.toString(bytemsg); 
System.out.println("client message a2sview: " + a2sview); 
+0

非常感謝Takahiko。你是對的它的工作;-)我真的應該更詳細地閱讀原始的Java文檔,但現在我只是想讓事情倉促完成。感謝您以前的電子郵件並在這裏​​回答。 – howToDeleteMyAccount