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

View File

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

View File

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

View File

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