use of com.neovisionaries.ws.client.WebSocketState 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);
}
}
Aggregations