2016-11-25 141 views
0

我正在製作一個有遊戲的網站。對於我需要發送數據tockets的遊戲。一切工作正常加載頁面,但我無法握手工作。如何完成WebSocket握手?

class ServerClient { 
    public ServerClient() { 
     handshake(); 
    } 

    private void handshake() { 
    try { 
     String line; 
     String key = ""; 
     boolean socketReq = false; 
     while (true) { 
      line = input.readLine(); 
      if (line.startsWith("Upgrade: websocket")) 
       socketReq = true; 
      if (line.startsWith("Sec-WebSocket-Key: ")) 
       key = line; 
      if (line.isEmpty()) 
       break; 
     } 
     if (!socketReq) 
      socket.close(); 
     String encodedKey = DatatypeConverter.printBase64Binary(
      MessageDigest.getInstance("SHA-1") 
       .digest((key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").getBytes())); 

     System.out.println(encodedKey); 
     output.println("HTTP/1.1 101 Switching Protocols"); 
     output.println("Upgrade: websocket"); 
     output.println("Connection: Upgrade"); 
     output.println("Sec-WebSocket-Accept: " + encodedKey); 
     output.flush(); 
     output.close();  // output = new PrintWriter(
          //  Socket.getOutputStream()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
} 

socketReq變量存在,因爲我不希望任何人直接從他們的瀏覽器連接到localhost:25580。 我的發送和接收函數在不同的線程中,並且它們將在握手之後啓動。

新的WebSocket的結果( 「WS://本地主機:25580」)在JS是

WebSocket連接到 'WS://本地主機:25580 /' 失敗:WebSocket的握手期間錯誤:淨:: ERR_CONNECTION_CLOSED

WebSocket的握手期間我有

錯誤:不正確的 '二段的WebSocket - 接受' 標題值

但我想我改變了代碼中的東西。

我搜索小時trought Article 1Article 2和來自其他站點。只是無法讓整個事情正常工作。

我沒有拿到鑰匙的點,爲什麼我們要對其進行編碼。

套接字已連接到瀏覽器,我正在從它獲取標題。

主機:本地主機:25580

二段的WebSocket密鑰:iWLnXKrA3fLD6h6UGMIigg ==

如何握手工作?

+1

我強烈建議你爲Java中的webSocket服務器提供預構建類(有很多)。還有很多比你的代碼更多。 – jfriend00

+1

這只是代碼的25580端口部分。我想這樣做沒有額外的圖書館。 – Juubes

回答

0

完成!

發現從http://blog.honeybadger.io/building-a-simple-websockets-server-from-scratch-in-ruby/的答案,它的工作完美!

代碼:

public ClientSocket(Socket socket) { 
    try { 
     this.socket = socket; 
     this.input = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
     this.output = new PrintWriter(socket.getOutputStream()); 
     handshake(); 
} 

private void handshake() { 
    try { 
     String line; 
     String key = ""; 
     while (true) { 
      line = input.readLine(); 
      if (line.startsWith("Sec-WebSocket-Key: ")) { 
       key = line.split(" ")[1]; 
       System.out.println("'" + key + "'"); 
      } 
      if (line == null || line.isEmpty()) 
       break; 
     } 
     output.println("HTTP/1.1 101 Switching Protocols"); 
     output.println("Upgrade: websocket"); 
     output.println("Connection: Upgrade"); 
     output.println("Sec-WebSocket-Accept: " + encode(key)); 
     output.println(); 
     output.flush(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

private String encode(String key) throws Exception { 
    key += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; 
    byte[] bytes = MessageDigest.getInstance("SHA-1").digest(key.getBytes()); 
    return DatatypeConverter.printBase64Binary(bytes); 
} 

現在我只需要消息進行解碼。

0

你得到一個net::ERR_CONNECTION_CLOSED錯誤,因爲你正在縮小與output.close()連接。

如果你想保持連接打開,顯然不握手期間關閉。