2015-12-29 58 views

回答

1

您需要做的第一件事就是知道如何連接並建立有效的WebSocket連接。這包括連接,升級請求和握手來完成交易。 (保持插座活着VS標準HTTP GET封閉發送後的插座上。然後,您需要調用回聲網址..看到這個主要的例子..

/** 
* Quick echo test code. 
* @param args 
*/ 
public static void main(String[] args) { 
    try { 
     HashMap<String, String> headers = new HashMap<String, String>(); 
     headers.put("key1", "value1"); 
     headers.put("key2", "value2"); 

     WebSocket ws = new WebSocket(new URI("ws://localhost:8080/echo")); 
     ws.setHeaders(headers); 
     ws.connect(); 

     String request = "Hello"; 
     ws.send(request); 
     String response = ws.recv(); 
     System.out.println(request); 
     if (request.equals(response)) { 
      System.out.print("Success!"); 
     } else { 
      System.out.print("Failed!"); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

如何在這裏good example完整的參考

我顯示了一個工作自包含從Java客戶端的觀點回聲Web類的回聲類