简体   繁体   English

Java WebSocket服务器OutputStream没有刷新

[英]Java WebSocket server OutputStream not flushing

I have read a similar question , but my problem wasn't solved. 我读过类似的问题 ,但我的问题没有解决。

I am trying, for the sake of learning, to build my own Java WebSocket server. 为了学习起见,我正在尝试构建自己的Java WebSocket服务器。 The server is set up fine, it accepts incoming connections and gets the handshake data from the client. 服务器设置正常,它接受传入连接并从客户端获取握手数据。 My server then calculates the handshake return data and tries to write it and flush it. 然后我的服务器计算握手返回数据并尝试写入并刷新它。 Nonetheless, the in the web inspector, no response headers are shown for the client and the onopen -JavaScript event is never fired. 尽管如此,在Web检查,没有响应头显示的客户端和onopen -JavaScript事件从来没有发射。

String EOL = System.getProperty("line.separator"); // actually a class-defined constant

BufferedReader inputStream = currentClient.getInputStream();
OutputStream outputStream = currentClient.getOutputStream();

String inputLine;
String handshake = "";

try {

    if(!inputStream.ready()){ continue; }

    System.out.println("Receiving:\n");

    while ((inputLine = inputStream.readLine()).length() > 0) {

        if(inputLine.startsWith("Sec-WebSocket-Key: ")){

            String inputKey = inputLine.replace("Sec-WebSocket-Key: ", "");
            String outputKey = WebSocket.getWebSocketKey(inputKey);

            handshake += "HTTP/1.1 101 Switching Protocols"+EOL;
            handshake += "Upgrade: websocket"+EOL;
            handshake += "Connection: Upgrade"+EOL;
            handshake += "Sec-WebSocket-Accept: "+outputKey;

        }

        System.out.println(inputLine);

    }

} catch (Exception e) {

    e.printStackTrace();

}

System.out.println("\n\nSending:\n");

System.out.println(handshake);
try {
    outputStream.write(handshake.getBytes(Charset.forName("UTF-8")));
    outputStream.flush();
} catch (IOException e) {
    e.printStackTrace();
}

So here's an example of what I get: 所以这是我得到的一个例子:

GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:65432
Origin: http://localhost
Sec-WebSocket-Key: ph1CO1PCF60uojeP+nql5A==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: x-webkit-deflate-frame

And what I try to send: 我尝试发送的内容:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: Z2Vy9p7Lp+MZPdZOe+L5GhVBDpc=

I'd like to note that sending the headers I send ought to be sufficient, since in a PHP WebSocket server I developed, sending no more than these headers DOES work. 我想请注意,发送我发送的标题应该足够了,因为在我开发的PHP WebSocket服务器中,发送的只不过这些标题可行。

A websocket handshake is a HTTP request followed by a HTTP response. websocket握手是HTTP请求,后跟HTTP响应。 RFC2616 states that the end-of-line marker for HTTP is CRLF ("\\r\\n"). RFC2616声明HTTP的行尾标记是CRLF(“\\ r \\ n”)。

HTTP requests end with a double newline ("\\r\\n\\r\\n" - see section 4 of the RFC ); HTTP请求以双换行结束(“\\ r \\ n \\ r \\ n” - 请参阅RFC第4节); the websocket handshake response is a HTTP response so also needs to end like this. websocket握手响应是一个HTTP响应,所以也需要像这样结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM