1. 说明
使用 webSocket 创建链接。
参考:https://blog.csdn.net/l_lhc/article/details/68950278
2. 添加步骤
- gradle 添加依赖库
compile 'io.socket:socket.io-client:0.8.3'
- 初始化 Socket
private io.socket.client.Socket mSocket;
{
try {
mSocket = IO.socket("http://www.baidu.com");
} catch (URISyntaxException e) {}
}
- 初始调用
mSocket.on("new message", onReceiveService);
mSocket.connect();
onReceiveService 是回调监听
- 回调监听
private Emitter.Listener onReceiveService = new Emitter.Listener() {
@Override
public void call(Object... args) {
JSONObject data = (JSONObject) args[0];
Log.i("Wooo", "onNewMessage receiver : " + data.toString());
}
};
- 消息发送
private void attemptSend() {
Log.i("Wooo", "attemptSend : ");
mSocket.emit("new message", "1234561234566666666666666666666666666666666666");
Log.i("Wooo", "attemptSend : success?????");
}