Search in sources :

Example 1 with WebSocketListener

use of com.neovisionaries.ws.client.WebSocketListener in project Slide by ccrama.

the class LiveThread method doLiveThreadUpdates.

public void doLiveThreadUpdates() {
    final PaginatorAdapter adapter = new PaginatorAdapter(this);
    baseRecycler.setAdapter(adapter);
    doLiveSidebar();
    if (thread.getWebsocketUrl() != null && !thread.getWebsocketUrl().isEmpty()) {
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                final ObjectReader o = new ObjectMapper().reader();
                try {
                    com.neovisionaries.ws.client.WebSocket ws = new WebSocketFactory().createSocket(thread.getWebsocketUrl());
                    ws.addListener(new WebSocketListener() {

                        @Override
                        public void onStateChanged(com.neovisionaries.ws.client.WebSocket websocket, WebSocketState newState) throws Exception {
                        }

                        @Override
                        public void onConnected(com.neovisionaries.ws.client.WebSocket websocket, Map<String, List<String>> headers) throws Exception {
                        }

                        @Override
                        public void onConnectError(com.neovisionaries.ws.client.WebSocket websocket, WebSocketException cause) throws Exception {
                        }

                        @Override
                        public void onDisconnected(com.neovisionaries.ws.client.WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) throws Exception {
                        }

                        @Override
                        public void onFrame(com.neovisionaries.ws.client.WebSocket websocket, WebSocketFrame frame) throws Exception {
                        }

                        @Override
                        public void onContinuationFrame(com.neovisionaries.ws.client.WebSocket websocket, WebSocketFrame frame) throws Exception {
                        }

                        @Override
                        public void onTextFrame(com.neovisionaries.ws.client.WebSocket websocket, WebSocketFrame frame) throws Exception {
                        }

                        @Override
                        public void onBinaryFrame(com.neovisionaries.ws.client.WebSocket websocket, WebSocketFrame frame) throws Exception {
                        }

                        @Override
                        public void onCloseFrame(com.neovisionaries.ws.client.WebSocket websocket, WebSocketFrame frame) throws Exception {
                        }

                        @Override
                        public void onPingFrame(com.neovisionaries.ws.client.WebSocket websocket, WebSocketFrame frame) throws Exception {
                        }

                        @Override
                        public void onPongFrame(com.neovisionaries.ws.client.WebSocket websocket, WebSocketFrame frame) throws Exception {
                        }

                        @Override
                        public void onTextMessage(com.neovisionaries.ws.client.WebSocket websocket, String s) throws Exception {
                            LogUtil.v("Recieved" + s);
                            if (s.contains("\"type\": \"update\"")) {
                                try {
                                    LiveUpdate u = new LiveUpdate(o.readTree(s).get("payload").get("data"));
                                    updates.add(0, u);
                                    runOnUiThread(new Runnable() {

                                        @Override
                                        public void run() {
                                            adapter.notifyItemInserted(0);
                                            baseRecycler.smoothScrollToPosition(0);
                                        }
                                    });
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            } else if (s.contains("embeds_ready")) {
                                String node = updates.get(0).getDataNode().toString();
                                LogUtil.v("Getting");
                                try {
                                    node = node.replace("\"embeds\":[]", "\"embeds\":" + o.readTree(s).get("payload").get("media_embeds").toString());
                                    LiveUpdate u = new LiveUpdate(o.readTree(node));
                                    updates.set(0, u);
                                    runOnUiThread(new Runnable() {

                                        @Override
                                        public void run() {
                                            adapter.notifyItemChanged(0);
                                        }
                                    });
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        /* todoelse if(s.contains("delete")){
                                    updates.remove(0);
                                    adapter.notifyItemRemoved(0);
                                }*/
                        }

                        @Override
                        public void onBinaryMessage(com.neovisionaries.ws.client.WebSocket websocket, byte[] binary) throws Exception {
                        }

                        @Override
                        public void onSendingFrame(com.neovisionaries.ws.client.WebSocket websocket, WebSocketFrame frame) throws Exception {
                        }

                        @Override
                        public void onFrameSent(com.neovisionaries.ws.client.WebSocket websocket, WebSocketFrame frame) throws Exception {
                        }

                        @Override
                        public void onFrameUnsent(com.neovisionaries.ws.client.WebSocket websocket, WebSocketFrame frame) throws Exception {
                        }

                        @Override
                        public void onError(com.neovisionaries.ws.client.WebSocket websocket, WebSocketException cause) throws Exception {
                        }

                        @Override
                        public void onFrameError(com.neovisionaries.ws.client.WebSocket websocket, WebSocketException cause, WebSocketFrame frame) throws Exception {
                        }

                        @Override
                        public void onMessageError(com.neovisionaries.ws.client.WebSocket websocket, WebSocketException cause, List<WebSocketFrame> frames) throws Exception {
                        }

                        @Override
                        public void onMessageDecompressionError(com.neovisionaries.ws.client.WebSocket websocket, WebSocketException cause, byte[] compressed) throws Exception {
                        }

                        @Override
                        public void onTextMessageError(com.neovisionaries.ws.client.WebSocket websocket, WebSocketException cause, byte[] data) throws Exception {
                        }

                        @Override
                        public void onSendError(com.neovisionaries.ws.client.WebSocket websocket, WebSocketException cause, WebSocketFrame frame) throws Exception {
                        }

                        @Override
                        public void onUnexpectedError(com.neovisionaries.ws.client.WebSocket websocket, WebSocketException cause) throws Exception {
                        }

                        @Override
                        public void handleCallbackError(com.neovisionaries.ws.client.WebSocket websocket, Throwable cause) throws Exception {
                        }

                        @Override
                        public void onSendingHandshake(com.neovisionaries.ws.client.WebSocket websocket, String requestLine, List<String[]> headers) throws Exception {
                        }
                    });
                    ws.connect();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (WebSocketException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }
}
Also used : WebSocketListener(com.neovisionaries.ws.client.WebSocketListener) WebSocketException(com.neovisionaries.ws.client.WebSocketException) IOException(java.io.IOException) WebSocketException(com.neovisionaries.ws.client.WebSocketException) IOException(java.io.IOException) LiveUpdate(net.dean.jraw.models.LiveUpdate) WebSocketFactory(com.neovisionaries.ws.client.WebSocketFactory) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) WebSocketFrame(com.neovisionaries.ws.client.WebSocketFrame) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) WebSocketState(com.neovisionaries.ws.client.WebSocketState)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 WebSocketException (com.neovisionaries.ws.client.WebSocketException)1 WebSocketFactory (com.neovisionaries.ws.client.WebSocketFactory)1 WebSocketFrame (com.neovisionaries.ws.client.WebSocketFrame)1 WebSocketListener (com.neovisionaries.ws.client.WebSocketListener)1 WebSocketState (com.neovisionaries.ws.client.WebSocketState)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 LiveUpdate (net.dean.jraw.models.LiveUpdate)1