|
|
|
@ -4,6 +4,7 @@ import com.terraocean.plugin.Settings
|
|
|
|
|
import com.terraocean.plugin.Strings
|
|
|
|
|
import com.terraocean.plugin.instance
|
|
|
|
|
import hazae41.minecraft.kutils.bukkit.info
|
|
|
|
|
import hazae41.minecraft.kutils.bukkit.schedule
|
|
|
|
|
import hazae41.minecraft.kutils.bukkit.warning
|
|
|
|
|
import okhttp3.*
|
|
|
|
|
import org.bukkit.Bukkit
|
|
|
|
@ -13,14 +14,13 @@ val client = OkHttpClient.Builder()
|
|
|
|
|
.readTimeout(0, TimeUnit.MILLISECONDS)
|
|
|
|
|
.build()
|
|
|
|
|
|
|
|
|
|
lateinit var socket: WebSocket
|
|
|
|
|
|
|
|
|
|
fun establishConnection() {
|
|
|
|
|
instance.info("establishing connection to ${Settings.socketURL}...")
|
|
|
|
|
val request = Request.Builder()
|
|
|
|
|
private var request = Request.Builder()
|
|
|
|
|
.url(Settings.socketURL)
|
|
|
|
|
.build()
|
|
|
|
|
lateinit var socket: WebSocket
|
|
|
|
|
var wsFails = 0
|
|
|
|
|
|
|
|
|
|
fun establishConnection() {
|
|
|
|
|
socket = client.newWebSocket(request, WSListener())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -67,9 +67,17 @@ class WSListener: WebSocketListener() {
|
|
|
|
|
|
|
|
|
|
override fun onOpen(webSocket: WebSocket, response: Response) {
|
|
|
|
|
instance.info("Socket connection established to $response.")
|
|
|
|
|
wsFails = 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
|
|
|
|
|
TODO("Automatic reconnection")
|
|
|
|
|
if (wsFails < 5) {
|
|
|
|
|
wsFails++
|
|
|
|
|
instance.warning("WS Fail: ${t.message}")
|
|
|
|
|
instance.info("Reconnecting to socket...")
|
|
|
|
|
}
|
|
|
|
|
instance.schedule(delay = 5, unit = TimeUnit.SECONDS) {
|
|
|
|
|
establishConnection()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|