websocket是web端的一个长连接协议,也是用来实时通知的一门技术,在不刷手动新界面的情况下可以实时刷新界面,比如一个报警触发,web端要实时弹窗提示,这个情况下就需要websocket了,现在先来谈谈android上如果使用websocket。
1.添加依赖
implementation 'org.java-websocket:Java-WebSocket:1.3.9'
2.使用封装类WebSocketHelper
import android.util.Log;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;
import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class WebSocketHelper {
private static final String TAG = "websocket";
private MyWebSocket myWebSocket;
private static class SingletonHolder {
private static final WebSocketHelper instance = new WebSocketHelper();
}
private WebSocketHelper() {
}
public static WebSocketHelper getInstance() {
return WebSocketHelper.SingletonHolder.instance;
}
private boolean isRunning = false;
private boolean isInterrupt