| | |
| | | import android.app.Activity |
| | | import android.app.Application |
| | | import android.os.Bundle |
| | | import cn.sinata.rxnetty.NettyClient |
| | | import cn.sinata.xldutils.application.BaseApplication |
| | | import cn.sinata.xldutils.utils.SPUtils |
| | | import com.dollearn.student.network.Apis |
| | |
| | | UMConfigure.setLogEnabled(true) |
| | | PlatformConfig.setWeixin(Const.WX_APP_ID, Const.WX_SECRET) |
| | | PlatformConfig.setQQZone(Const.QQ_APP_ID, Const.QQ_SECRET) |
| | | initNetty() |
| | | } |
| | | |
| | | private fun initNetty() { |
| | | NettyClient.getInstance().init(this, Apis.SOCKET_SERVER, Apis.SOCKET_PORT, false) |
| | | NettyClient.getInstance().addOnMessageListener { message -> |
| | | try { |
| | | val json = JSONObject(message) |
| | | val method = json.optString("method") |
| | | val code = json.optString("code") |
| | | if (code == "200"){ |
| | | when (method) { |
| | | Const.Method.PING_RECEIVE -> {//心跳 |
| | | //延时发送心跳 |
| | | if (heartSubscribe != null && !heartSubscribe?.isDisposed!!) { |
| | | heartSubscribe?.dispose() |
| | | } |
| | | heartSubscribe = |
| | | Flowable.just("").delay(5000, TimeUnit.MILLISECONDS).subscribe { |
| | | sendHeart() |
| | | } |
| | | } |
| | | |
| | | // Const.Method.OFFLINE -> {//强制下线 |
| | | // val topAct = activities[activities.lastIndex] |
| | | // SPUtils.instance().remove(Const.User.TOKEN).remove(Const.User.USER_ID).remove(Const.User.NO_PHONE).apply() |
| | | // val tipDialog = TipDialog() |
| | | // tipDialog.arguments = bundleOf("msg" to "您的账号已在其他地方登陆,请重新登陆","isAlert" to true) |
| | | // tipDialog.setDismissCallback(object :TipDialog.OnDismiss{ |
| | | // override fun onDismiss() { |
| | | // exit() |
| | | // topAct.startActivity<LoginActivity>() |
| | | // } |
| | | // }) |
| | | // tipDialog.show((topAct as BaseActivity).supportFragmentManager,"logout") |
| | | // } |
| | | } |
| | | } |
| | | }catch (e: Exception){ |
| | | e.printStackTrace() |
| | | } |
| | | } |
| | | NettyClient.getInstance().setOnConnectListener { |
| | | //连接成功,发送一次心跳 |
| | | sendHeart() |
| | | } |
| | | } |
| | | |
| | | private fun sendHeart() { |
| | | val userId = SPUtils.instance().getString(Const.User.USER_ID) |
| | | val token = SPUtils.instance().getString(Const.User.TOKEN) |
| | | if (token.isNotEmpty()) { |
| | | NettyClient.getInstance() |
| | | .sendMessage("{\"data\":{\"userId\":$userId,\"token\":\"$token\",\"type\":1},\"method\":\"PING\",\"code\":200,\"msg\":\"SUCCESS\"}") |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 关闭所有页面 |