Android WebSocket, Data From Server
How can I get response from WebSocket server?? webserver is on the http://www.websocket.org/echo.html
Solution 1:
The first thing you need to do is know how to connect and establish a valid WebSocket connection. This involves a connect, an upgrade request, and a handshake to seal the deal. (to keep the socket alive vs a standard HTTP GET that closes the socket after send. You then need to call the echo url.. See this main example..
/**
* 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();
}
}
The complete reference on how to is here good example
I shows a working self contained class of an echo from a Java Client point of view to a WebSocket
Post a Comment for "Android WebSocket, Data From Server"