使用的依赖库
compile 'com.neovisionaries:nv-websocket-client:1.30'
public class WsClientTool implements WebSocketListener {
private static WsClientTool singleton;
private WebSocket ws;
private static final String TAG = "WsClientTool";
private List<WsMessageListener> listeners = new ArrayList<>();
private String url;
private String token;
private WsClientTool() {
}
public static WsClientTool getSingleton() {
if (singleton == null) {
synchronized (RestfulTools.class) {
if (singleton == null) {
singleton = new WsClientTool();
}
}
}
return singleton;
}
public void addMessageListener(WsMessageListener listener) {
Log.i(TAG,"ws.addMessageListener()");
if(null != listener && !listeners.contains(listener)) {
listeners.add(listener);
}
}
public void removeMessageListener(WsMessageListener listener) {
if(null != listener) {
listeners.remove(listener);
}
}
public void connect(String serverUrl, String token) {
url = serverUrl;
this.token = token;
try {
if(null == ws) {
ws = new WebSocketFactory().createSocket(serverUrl);
ws.addHeader("opera-token", token);
ws.setPingInterval(60 * 1000);
ws.addListener(this);
Log.i(TAG,"ws.connectAsynchronously() is null="+ws.toString());
} else {
ws = ws.recreate();
Log.i(TAG,"ws.connectAsynchronously() is not null="+ws.toString());
}
ws.connectAsynchronously();
} catch (IOException e) {
e.printStackTrace();
}
}
public void reconnect() {
if(null != ws && !ws.isOpen()) {
ws.disconnect();
ws = null;
connect(url, token);
}
}
public void disconnect() {
if(null != ws) {
ws.disconnect();
}
}
public boolean isConnected() {
return null != ws && ws.isOpen();
}
public void sendText(String content) {
if(null != ws && ws.isOpen()) {
Log.i(TAG,"wsclient sendText ="+content);
ws.sendText(content);
}
}
@Override
public void onStateChanged(WebSocket websocket, WebSocketState newState) throws Exception {
Log.i(TAG,"ws.onStateChanged newState="+newState.name());
}
@Override
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
Log.d(TAG, "connected");
for (WsMessageListener listener: listeners) {
listener.onConnected();
}
}
@Override
public void onConnectError(WebSocket websocket, WebSocketException cause) throws Exception {
Log.d(TAG, "connect error");
cause.printStackTrace();
}
@Override
public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) throws Exception {
Log.d(TAG, "disconnected");
for (WsMessageListener listener: listeners) {
listener.onDisconnected();
}
}
@Override
public void onFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
Log.d(TAG, "onFrame: " + frame.toString());
}
@Override
public void onContinuationFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
}
@Override
public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
Log.d(TAG, "onTextFrame: " + frame.toString());
}
@Override
public void onBinaryFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
}
@Override
public void onCloseFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
}
@Override
public void onPingFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
}
@Override
public void onPongFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
}
@Override
public void onTextMessage(WebSocket websocket, String text) throws Exception {
Log.i(TAG, "onTextMessage: " + text);
for (WsMessageListener listener: listeners) {
listener.onTextMessage(text);
}
}
@Override
public void onBinaryMessage(WebSocket websocket, byte[] binary) throws Exception {
}
@Override
public void onSendingFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
Log.i(TAG, "onSendingFrame");
}
@Override
public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception {
Log.i(TAG, "onFrameSent");
}
@Override
public void onFrameUnsent(WebSocket websocket, WebSocketFrame frame) throws Exception {
Log.i(TAG, "onFrameUnsent");
}
@Override
public void onError(WebSocket websocket, WebSocketException cause) throws Exception {
cause.printStackTrace();
Log.i(TAG, "onError");
}
@Override
public void onFrameError(WebSocket websocket, WebSocketException cause, WebSocketFrame frame) throws Exception {
Log.i(TAG, "onFrameError");
}
@Override
public void onMessageError(WebSocket websocket, WebSocketException cause, List<WebSocketFrame> frames) throws Exception {
Log.i(TAG,"ws.onMessageError ");
}
@Override
public void onMessageDecompressionError(WebSocket websocket, WebSocketException cause, byte[] compressed) throws Exception {
cause.printStackTrace();
Log.i(TAG,"ws.onMessageDecompressionError ");
}
@Override
public void onTextMessageError(WebSocket websocket, WebSocketException cause, byte[] data) throws Exception {
Log.i(TAG,"ws.onTextMessageError ");
}
@Override
public void onSendError(WebSocket websocket, WebSocketException cause, WebSocketFrame frame) throws Exception {
Log.i(TAG,"ws.onSendError ");
}
@Override
public void onUnexpectedError(WebSocket websocket, WebSocketException cause) throws Exception {
cause.printStackTrace();
Log.i(TAG,"ws.onUnexpectedError ");
}
@Override
public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception {
cause.printStackTrace();
Log.i(TAG,"ws.handleCallbackError ");
}
@Override
public void onSendingHandshake(WebSocket websocket, String requestLine, List<String[]> headers) throws Exception {
}
}
主要就是发送数据和数据的接收。