Java WebSocket服务器OutputStream不刷新 [英] Java WebSocket server OutputStream not flushing

查看:333
本文介绍了Java WebSocket服务器OutputStream不刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了类似的问题,但是我的问题不是解决.

I have read a similar question, but my problem wasn't solved.

为了学习,我正在尝试构建自己的Java WebSocket服务器.服务器设置良好,它接受传入的连接并从客户端获取握手数据.然后,我的服务器计算握手返回数据,并尝试将其写入并刷新.尽管如此,在Web检查器中,没有显示客户端的响应标头,并且从未触发onopen -JavaScript事件.

I am trying, for the sake of learning, to build my own Java WebSocket server. 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.

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

我要发送的内容:

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

我想指出的是,发送发送的标头应该足够了,因为在我开发的PHP WebSocket服务器中,发送的标头最多可以工作.

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.

推荐答案

websocket握手是一个HTTP请求,后跟一个HTTP响应. RFC2616 指出, HTTP为CRLF("\ r \ n").

A websocket handshake is a HTTP request followed by a HTTP response. RFC2616 states that the end-of-line marker for HTTP is CRLF ("\r\n").

HTTP请求以双换行符("\ r \ n \ r \ n"结尾-参见

HTTP requests end with a double newline ("\r\n\r\n" - see section 4 of the RFC); the websocket handshake response is a HTTP response so also needs to end like this.

这篇关于Java WebSocket服务器OutputStream不刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆