功能简介:

微吼云SDK 为移动应用提供一个完善的 IM 系统开发框架,可以使第三方用户快速集成并使用IM聊天等多种消息类型, 节约开发成本,IM SDK简易流程图如下:

备注: 如果使用IMSDK,需要先获取ChannelID 传送门 : TODO channel 获取地址

集成过程:

请先查看大目录下的工程配置,配置成功后在进行如下操作 : 本文将按照Android Studio 为默认工具进行讲解。 如果你,复制 vhallims1.0.jar到工程lib目录下。

1、 使用Gradle自动添加Jar包依赖。

2、 通过类库配置集成 SDK

1 创建VHIM实例

/*
 * mChannelId 频道ID 在IM中可以理解为聊天室的ID。
 * accessToken 是访问Token,使用此Token可以发送聊天内容。
 */
im = new VHIM(mChannelId, mAccessToken);

2添加监听

当VHIM创建完毕,需要添加监听消息,每当有消息推送会回调onMessage(),用户需要根据自己的场景解析数据

公共字段 描述
third_party_user_id 第三方用户ID
nick_name 用户名
avatar 头像
date_time 时间戳
user_online_num 当前在线人数
data 对应各个不同消息的数据
``
im.setOnMessageListener(new VHIM.OnMessageListener() {
            @Override
            public void onMessage(String msg) {
				if (event.equals(VhallConnectService.TYPE_CUSTOM)) {//收到自定义消息
                    //Do Something....
                } else if (event.equals(VhallConnectService.TYPE_CHAT)) { // 聊天消息
					//Do Something....
                } else if (event.equals(VhallConnectService.TYPE_JOIN)) { // 上线消息
					//Do Something....
                } else if (event.equals(VhallConnectService.TYPE_LEAVE)) {// 下线消息
                	//Do Something....
                }
            }
        });

3 进入聊天室

什么是聊天室:

1、 每一个Channel对应一个聊天室,用户可以根据Channel加入对应的聊天室。

2、 每一个聊天室允许加入聊天的成员无上限。

3、 允许不同的端(PC/移动)加入聊天室。

4、 聊天室只允许用户Join加入,不能邀请

5、 当退出聊天室后,IM服务将不会在给此用户推送消息。

第三方用户发起登录的流程,每次调用Join方法,加入聊天室,IM服务会自动发送一条上线消息,这条上线消息可以在OnMessage中接收。

 void join(); 

4 发送消息及回调

当用户加入聊天室后,可以发送消息,message为用户需要发送的内容,当调用此方法,发送的内容会通过IM服务让此聊天室中的所有用户看到。

  • message 需要发送的消息
  • callback 消息发送后的回调
/*
 * message 用户需要发送的消息
 * callback 会返回用户发送后的结果。需要重写IM中的Callback回调。
 */
 void sendMsg(String message , Callback callback);

5 离开聊天室

每次调用leave方法,离开聊天室,IM服务会自动发送一条下线消息,这条下线消息可以在OnMessage中接收。

 void leave();

SDK内部定义的错误码和常量

关于IM服务的常量定义在 Framework.jar包中的 VhallConnectService , 用户可以根据自己的实际场景去查看。

常量 描述
VhallConnectService.TYPE_CUSTOM 收到自定义消息
VhallConnectService.TYPE_CHAT 接收普通的聊天消息
VhallConnectService.TYPE_JOIN 接收上线消息
VhallConnectService.TYPE_LEAVE 接收下线消息

代码演示

public class IMActivity extends Activity {

    private String mChannelId = "";
    private String mAccessToken = "";
    private LinearLayout ll_content;
    private EditText et;
    VHIM im;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mChannelId = getIntent().getStringExtra("channelid");
        mAccessToken = getIntent().getStringExtra("token");
        setContentView(R.layout.im_layout);
        ll_content = (LinearLayout) this.findViewById(R.id.ll_content);
        et = (EditText) this.findViewById(R.id.et);
        et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                switch (actionId) {
                    case EditorInfo.IME_NULL:
                        System.out.println("null for default_content: " + v.getText());
                        break;
                    case EditorInfo.IME_ACTION_SEND:
                        im.sendMsg(v.getText().toString(), new Callback() {
                            @Override
                            public void onFailure(Call call, IOException e) {
                                L.e("im", e.getMessage());
                            }

                            @Override
                            public void onResponse(Call call, Response response) throws IOException {
                                L.e("im", response.body().string());
                            }
                        });
                        break;
                    case EditorInfo.IME_ACTION_DONE:
                        System.out.println("action done for number_content: " + v.getText());
                        break;
                }
                //Toast.makeText(this, v.getText()+"--" + actionId, Toast.LENGTH_LONG).show();
                return true;
            }
        });
        im = new VHIM(mChannelId, mAccessToken);
        im.setOnMessageListener(new VHIM.OnMessageListener() {
            @Override
            public void onMessage(String msg) {
                TextView textView = new TextView(IMActivity.this);
                textView.setText(msg);
                ll_content.addView(textView);
            }
        });
        im.join();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        im.leave();
    }
}