android socket io 前篇

地址:https://socket.io/blog/native-socket-io-and-android/

android studio 配置demo

1.配置依赖库文件:

  应用app/ build.gradle:

// app/build.gradle
dependencies {
    ...
    compile 'com.github.nkzawa:socket.io-client:0.3.0'
}

2.权限配置

 AndroidManifest.xml.

<!-- app/AndroidManifest.xml -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.INTERNET" />
    ...
</manifest>

3.使用Socket IO,注意引导的库文件

 (1)初始化IO Socket 

import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket;

private Socket mSocket;
{
    try {
        mSocket = IO.socket("http://chat.socket.io");
    } catch (URISyntaxException e) {}
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mSocket.connect();
}

IO.socket(CHAT_SERVER_URL, options);构造方法中参数 option中可以设置连接相关参数,里面

 // 是否自动重连

public boolean reconnection = true;
// 重连尝试次数
public int reconnectionAttempts;
// 重连间隔
public long reconnectionDelay;
// 最大连接等待时间
public long reconnectionDelayMax;
// 连接超时时间 (ms),设置为-1表示不超时
public long timeout = 20000;

(2)发送数据,用键值对形式发送mSocket.emit("new message", message); 

   可以单独发送整个内容mSocket.send(message);

private EditText mInputMessageView;

private void attemptSend() {
    String message = mInputMessageView.getText().toString().trim();
    if (TextUtils.isEmpty(message)) {
        return;
    }

    mInputMessageView.setText("");
    mSocket.emit("new message", message);
}

还可以这样:
mSocket.emit("new message", content, new Ack() { 
 @Override public void call(Object... args) { 
 
 } 
});

(3)监听服务器返回信息on监听事件,Emitter.Listener事件回调(注意引导的库文件

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // onNewMessage 来监听服务器发来的 "new message" 事件
    mSocket.on("new message", onNewMessage);
    mSocket.connect();
}
import com.github.nkzawa.emitter.Emitter;

private Emitter.Listener onNewMessage = new Emitter.Listener() {
    @Override
    public void call(final Object.. args) {        //在UI主线程调用并更新界面,call是子线程
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                JSONObject data = (JSONObject) args[0];
                String username;
                String message;
                try {
                    username = data.getString("username");
                    message = data.getString("message");
                } catch (JSONException e) {
                    return;
                }

                // add the message to view
                addMessage(username, message);
            }
        });
    }
};

(4)销毁socket

@Override
public void onDestroy() {
    super.onDestroy();
    //先断开服务器连接,再释放消息监听事件
    mSocket.disconnect();
    mSocket.off("new message", onNewMessage);
}


有相关的例子 好心人上传github,大家可以参考参考:

https://github.com/silencezwm/AndroidIMBySocketIO

https://github.com/nkzawa/socket.io-android-chat

https://github.com/xuduo/socket.io-push-android

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页