Java WebSocket OutputStream server does not flush

I read a similar question , but my problem was not resolved.

I am trying, for the sake of learning, to create my own Java WebSocket server. The server is configured perfectly, it accepts incoming connections and receives confirmation data from the client. Then my server calculates the acknowledgment return data and tries to write it and clear it. However, the client’s web inspector does not display response headers, and the onopen-JavaScript event never fires.

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 is 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'm trying to send:

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

I would like to note that sending the headers I am sending should be sufficient, since DOES works on the PHP WebSocket server that I developed by sending no more than these headers.

+1
1

websocket - HTTP-, HTTP-. RFC2616 , HTTP CRLF ( "\r\n" ).

HTTP- ( "\ r\n\r\n" - . 4 RFC); websocket HTTP, .

+3

All Articles