簡體   English   中英

為什么InputStreamReader無法讀取websocket包的內容?

[英]Why InputStreamReader cannot read the content of a websocket package?

為了了解websocket的行為,我在Java中創建了一個簡單的SocketServer來交換消息。 服務器應遵循以下操作:

1)在8080端口監聽

2)在瀏覽器客戶端上手動生成並由服務器接收的Websocket握手消息。

3)構建對握手消息的響應並回復客戶端

4)實際讀取具有相同連接的websocket信息字節。

該問題已在步驟4發生。當服務器以握手消息響應時,InputStreamReader不再能夠接收任何新消息。 即使客戶端已經發送了消息,它也會在readline()方法上阻止。 從wireshark中,我可以看到客戶端發送了消息並且服務器響應了ack。 任何幫助,將不勝感激,謝謝。

更新:我剛剛注意到這個問題已經問過了。 我將首先研究其他帖子上的建議。

更新:行為與此博文相同: 奇怪的網絡套接字行為:僅在關閉選項卡發送數據當網絡選項卡關閉時,讀取流接收到數據包。

Wireshark屏幕截圖:

tcp流跟蹤

包裹順序

日志:

inputline: GET / HTTP/1.1
inputline: Host: localhost:8080
inputline: Connection: Upgrade
inputline: Pragma: no-cache
inputline: Cache-Control: no-cache
inputline: Upgrade: websocket
inputline: Origin: file://
inputline: Sec-WebSocket-Version: 13
inputline: User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36
inputline: Accept-Encoding: gzip, deflate, br
inputline: Accept-Language: en,zh-TW;q=0.8,zh;q=0.6,zh-CN;q=0.4
inputline: Sec-WebSocket-Key: Yin4xn04vr9iBH1b2dU15A==
inputline: Sec-WebSocket-Extensions: permessage-deflate;    client_max_window_bits
inputline: 
response: HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: +Y9whLTzCdyN1INpAxjkO6yD2Nw=

服務器套接字代碼:

public class EchoServer {
public static String HTTP_VERSION_HEADER = "HTTP/1.1";
public static void main(String[] args) throws IOException {

    int portNumber = 8080;

    try (
        ServerSocket serverSocket =
            new ServerSocket(Integer.parseInt("8080"));
        Socket clientSocket = serverSocket.accept();     
        PrintWriter out =
            new PrintWriter(clientSocket.getOutputStream(), true);                   
        BufferedReader in = new BufferedReader(
            new InputStreamReader(clientSocket.getInputStream()));
    ) {
        String inputLine;
        StringBuilder sb = new StringBuilder();
        while ( true) {
                inputLine = in.readLine();
                if(inputLine == null) {
                    System.out.println("input is null");
                    continue;
                }
                System.out.println("inputline: " + inputLine);
                sb.append(inputLine).append(System.lineSeparator());
                if(inputLine.equals("")) {
                        Message msg = new Message(sb.toString(), new Date());
                        HttpMessage tmpMessage = new HttpMessage(msg.getText(), new Date());
                        String response = generateHandshakeResponse(tmpMessage);
                        System.out.println("response: " + response);
                        out.println(response);
                        out.flush();
                }
        }
    } catch (IOException e) {
        System.out.println("Exception caught when trying to listen on port "
            + portNumber + " or listening for a connection");
        System.out.println(e.getMessage());
    }
}

private static String generateHandshakeResponse(HttpMessage message) {
    String webSocketKey = message.getVal(HttpMessage.SEC_WEBSOCKET_KEY);
    String webSocketAccept = webSocketKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
    byte[] bytes = DigestUtils.sha1(webSocketAccept);
    String secWebSocketAcceptVal = Base64.encodeBase64String(bytes);

    StringBuilder sb = new StringBuilder();
    sb.append(HTTP_VERSION_HEADER).append(" 101 ").append("Switching Protocols\r\n");
    sb.append("Upgrade: websocket\r\n");
    sb.append("Connection: Upgrade\r\n");
    sb.append("Sec-WebSocket-Accept: ").append(secWebSocketAcceptVal).append("\r\n");
    sb.append("\n\n") //<--- this line fixed the problem
    //log.debug(sb.toString());
    return sb.toString();
    }
}

客戶端代碼:

<!doctype html>
<html lang="en">
<head>
<title>Websocket Client</title>
</head>
<body>
<script>
var exampleSocket = new WebSocket("ws://localhost:8080");
exampleSocket.onopen = function (event) {
  console.log("connection opened..");
  exampleSocket.send("Can you hear me?"); 
};
exampleSocket.onmessage = function (event) {
  console.log(event.data);
}

function sendMsg(){
  console.log("send message..");
  exampleSocket.send("hello hello");
}
</script>
<button onclick="sendMsg()" title="send">send</button>
</body>
</html>

非常感謝EJP,問題在於響應握手中缺少空白行以指示消息的結尾。

暫無
暫無

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

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