This commit is contained in:
tursom 2021-04-19 15:57:47 +08:00
parent a8ce78e176
commit 1294fcba16
4 changed files with 17 additions and 10 deletions

View File

@ -6,6 +6,7 @@ plugins {
dependencies {
api(project(":"))
api(project(":ts-core"))
api(project(":ts-core:ts-buffer"))
implementation(project(":ts-core:ts-xml"))
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2")
api(group = "com.squareup.retrofit2", name = "converter-gson", version = "2.9.0")
@ -14,6 +15,9 @@ dependencies {
// https://mvnrepository.com/artifact/org.jsoup/jsoup
api(group = "org.jsoup", name = "jsoup", version = "1.13.1")
testImplementation(project(":ts-core:ts-coroutine"))
}
@kotlin.Suppress("UNCHECKED_CAST")

View File

@ -1,7 +1,7 @@
package cn.tursom.http
import cn.tursom.utils.coroutine.MainDispatcher
import cn.tursom.utils.gson
import cn.tursom.core.Utils.gson
import cn.tursom.core.coroutine.MainDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

View File

@ -33,6 +33,8 @@ class WebSocketClient(
val maxContextLength: Int = 4096,
private val headers: Map<String, String>? = null,
private val handshakerUri: URI? = null,
val autoRelease: Boolean = true,
var initChannel: ((ch: SocketChannel) -> Unit)? = null
) {
private val uri: URI = URI.create(url)
internal var ch: Channel? = null
@ -68,10 +70,12 @@ class WebSocketClient(
headers?.forEach { (k, v) ->
httpHeaders[k] = v
}
val handshakerAdapter = WebSocketClientHandshakerAdapter(WebSocketClientHandshakerFactory.newHandshaker(
handshakerUri ?: uri, WebSocketVersion.V13, null, true, httpHeaders
), this, handler)
val handler = WebSocketClientChannelHandler(this, handler)
val handshakerAdapter = WebSocketClientHandshakerAdapter(
WebSocketClientHandshakerFactory.newHandshaker(
handshakerUri ?: uri, WebSocketVersion.V13, null, true, httpHeaders
), this, handler
)
val handler = WebSocketClientChannelHandler(this, handler, autoRelease)
val bootstrap = Bootstrap()
bootstrap.group(group)
.channel(NioSocketChannel::class.java)
@ -90,14 +94,12 @@ class WebSocketClient(
addLast(WebSocketClientCompressionHandler.INSTANCE)
}
addLast(handshakerAdapter)
//if (log) {
// addLast(LoggingHandler())
//}
addLast(handler)
if (autoWrap) {
addLast(WebSocketFrameWrapper)
}
}
initChannel?.invoke(ch)
}
})
bootstrap.connect(uri.host, port)

View File

@ -8,7 +8,8 @@ import io.netty.handler.codec.http.websocketx.*
class WebSocketClientChannelHandler(
val client: WebSocketClient,
val handler: WebSocketHandler,
) : SimpleChannelInboundHandler<WebSocketFrame>() {
autoRelease: Boolean = true,
) : SimpleChannelInboundHandler<WebSocketFrame>(autoRelease) {
override fun channelInactive(ctx: ChannelHandlerContext) {
handler.onClose(client)