簡體   English   中英

Java Websocket客戶端庫問題:nv-websocket-client sendBinary

[英]Java Websocket client lib issue: nv-websocket-client sendBinary

當從客戶端使用此lib的sendBinary時,我在服務器上獲得了全0;

此頁面要求我添加更多描述,但是下面的代碼簡單明了,應該可以自我解釋...

客戶端代碼:

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:

非常感謝你的幫助。

在服務器端on_message方法,你要的內容復制messagebytemsg傳遞之前bytemsgArray.toString

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM