Search in sources :

Example 1 with Draft_17

use of org.java_websocket.drafts.Draft_17 in project ChatExchange by HueToYou.

the class WebSocketBackend method createWebSocket.

/**
     * Creates the WebSocket using the provided URI
     * @param uri WebSocket URI
     */
private void createWebSocket(URI uri) {
    Map<String, String> headers = new HashMap<>();
    headers.put("Cookie", mRequestFactory.cookies());
    headers.put("Origin", "https://chat.stackexchange.com");
    mWebSocketClient = new WebSocketClient(uri, new Draft_17(), headers, 0) {

        @Override
        public void onOpen(ServerHandshake serverHandshake) {
            Log.i(TAG, "WebSocket connection opened");
        }

        @Override
        public void onMessage(String s) {
            for (Event event : mGson.fromJson(s, WsMessage.class).events) {
                mBroadcaster.broadcastEvent(event);
            }
        }

        @Override
        public void onClose(int i, String s, boolean b) {
        // TODO
        }

        @Override
        public void onError(Exception e) {
            Log.e(TAG, e.getMessage());
        // TODO
        }
    };
}
Also used : ServerHandshake(org.java_websocket.handshake.ServerHandshake) HashMap(java.util.HashMap) Draft_17(org.java_websocket.drafts.Draft_17) WebSocketClient(org.java_websocket.client.WebSocketClient) JsonParseException(com.google.gson.JsonParseException) JsonSyntaxException(com.google.gson.JsonSyntaxException) URISyntaxException(java.net.URISyntaxException)

Aggregations

JsonParseException (com.google.gson.JsonParseException)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 WebSocketClient (org.java_websocket.client.WebSocketClient)1 Draft_17 (org.java_websocket.drafts.Draft_17)1 ServerHandshake (org.java_websocket.handshake.ServerHandshake)1