我看过一个类似的问题 ,但并没有解决我的问题。
我想,对于学习的缘故,建立自己的Java的WebSocket服务器。 该服务器设置了罚款,它接受传入的连接,并从客户端获取握手数据。 我的服务器然后计算握手返回数据,并试图把它写和冲洗掉。 尽管如此,在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();
}
因此,这里是什么,我得到一个例子:
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服务器,发送不超过这些标题确实工作。