侦听套接字打开情况,然后再发送回服务器

时间:2019-08-02 13:40:07

标签: android kotlin java-websocket android-websocket

我正在制作一个向我发送设备位置的应用程序,并通知设备是否在15分钟内没有移动,所以我写了以下内容:

import android.content.Context
import androidx.lifecycle.MutableLiveData
import org.java_websocket.client.WebSocketClient
import org.java_websocket.handshake.ServerHandshake
import java.lang.Exception
import java.net.URI

class SocketClient {
    val uri = URI("ws://10.0.2.2:8080/ws")

    val message = MutableLiveData<String>()

    fun connectWebSocket(context: Context) {

        mWebSocketClient = object : WebSocketClient(uri) {
            override fun onError(ex: Exception?) {}

            override fun onClose(code: Int, reason: String?, remote: Boolean) {}

            override fun onOpen(serverHandshake: ServerHandshake) {}

            override fun onMessage(s: String) {
                message.postValue(s)
            }
        }
        mWebSocketClient.connect()
    }
}

警报不动:

import android.widget.Toast
import android.content.Intent
import android.content.BroadcastReceiver
import android.content.Context

class AppAlarm : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        Toast.makeText(context, "Alarm Triggered", Toast.LENGTH_SHORT).show()
    }
}

位置广播:

import android.app.AlarmManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.SystemClock
import android.widget.Toast
import com.google.android.gms.location.LocationResult
import org.json.JSONObject


class AppLocationService  : BroadcastReceiver() {

    val ACTION_PROCESS_UPDATE = "oryx.track.UPDATE_LOCATION"
    override fun onReceive(p0: Context, p1: Intent?) {
        if (p1 != null) {
            val action = p1.action
            if (ACTION_PROCESS_UPDATE == action){
                var result = LocationResult.extractResult(p1)
                if (result != null){
                    alarmMgr?.cancel(alarmIntent)

                    location = result.lastLocation

                    val jsonStr =
                        "{\"unique_id\": $uniqueID, \"lat\": ${location.latitude}, \"lon\": ${location.longitude}, \"speed\": ${location.speed}}"
                    val jObj = JSONObject(jsonStr)
                    Toast.makeText(p0, "latitude = ${location.latitude}",Toast.LENGTH_LONG).show()
                    if (mWebSocketClient.isOpen) {
                        mWebSocketClient.send(jObj.toString())
                    } else {
                        try {
                            client.connectWebSocket(p0)
                            mWebSocketClient.send(jObj.toString())
                        } catch (e : Exception){
                            Toast.makeText(p0, "not connected $e", Toast.LENGTH_LONG).show()
                        }
                    }
                    alarmMgr?.setExactAndAllowWhileIdle(
                        AlarmManager.ELAPSED_REALTIME,
                        SystemClock.elapsedRealtime()+ AlarmManager.INTERVAL_FIFTEEN_MINUTES, alarmIntent)
                }
            }
        }
    }
}

我遇到了以下代码:

try {
    client.connectWebSocket(p0)
    mWebSocketClient.send(jObj.toString())
}

如果服务器关闭,并且在稍后阶段启动,则套接字需要花费一些时间来执行client.connectWebSocket(p0),因此在打开连接之前将执行下一条语句mWebSocketClient.send(jObj.toString()),因此实际上什么也没做发送给服务器!

0 个答案:

没有答案