|
| 1 | +package io.jiantao.androidsocketchat; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | + |
| 5 | +import com.qpdstudio.logger.util.LogUtil; |
| 6 | + |
| 7 | +import java.nio.charset.Charset; |
| 8 | +import java.util.Random; |
| 9 | + |
| 10 | +import io.jiantao.socketclient.ImManager; |
| 11 | +import io.jiantao.socketclient.core.Constants; |
| 12 | +import io.jiantao.socketclient.core.ImBaseReceiver; |
| 13 | + |
| 14 | +/** |
| 15 | + * Created by jiantao on 2017/4/3. |
| 16 | + * test code |
| 17 | + */ |
| 18 | + |
| 19 | +public class MedImReceiver extends ImBaseReceiver { |
| 20 | + private static final String TAG = Constants.getIMCoreLogTag(MedImReceiver.class.getSimpleName()); |
| 21 | + |
| 22 | + static final Random randomTest = new Random(); |
| 23 | + |
| 24 | + @Override |
| 25 | + protected void onReceiveSocketLocalMsg(Context context, int msgWhat) { |
| 26 | + LogUtil.i(TAG, " onReceiveSocketLocalMsg msgWhat = %d", msgWhat); |
| 27 | + switch (msgWhat) { |
| 28 | + case Constants.LOCAL_MSG_CONNECT: |
| 29 | + // socket连接完成 |
| 30 | + break; |
| 31 | + |
| 32 | + case Constants.LOCAL_MSG_DISCONNECT: |
| 33 | + case Constants.LOCAL_MSG_CONNECT_FAILED: |
| 34 | + //内部连接失败,或者通知断开连接,这里统一重连 |
| 35 | + //延迟5s重连 , 这里不判断网络状态,就不用监听网络变化。 |
| 36 | +// Observable.timer(5, TimeUnit.SECONDS) |
| 37 | +// .observeOn(AndroidSchedulers.mainThread()) |
| 38 | +// .subscribe(new Action1<Long>() { |
| 39 | +// @Override |
| 40 | +// public void call(Long aLong) { |
| 41 | +// } |
| 42 | +// }); |
| 43 | + ImManager.init(context,-1); |
| 44 | + break; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + protected void onReceiveSocketRemoteMsg(Context context, int msgType, byte[] byteArrayExtra) { |
| 50 | + final String msg = new String(byteArrayExtra, Charset.defaultCharset()); |
| 51 | + LogUtil.i(TAG, "msgType = %d, msg = %s",msgType, msg); |
| 52 | + //消息回执 |
| 53 | + System.out.println("client <<< 收到服务器消息: \""+msg+"\""); |
| 54 | + ImManager.sendMessage(context, (" client 发送de 消息回执 ").getBytes()); |
| 55 | + ImManager.sendMessage(context, (" client 发送新消息 id = "+ randomTest.nextInt(100)).getBytes()); |
| 56 | + } |
| 57 | +} |
0 commit comments