mirror of
https://github.com/tursom/TursomServer.git
synced 2025-04-02 15:41:23 +08:00
add WebSocketFrameWrapper
This commit is contained in:
parent
06ff1a54f5
commit
b5a5f9f5e6
@ -1,5 +1,5 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
ext.kotlinVersion = '1.4.0'
|
ext.kotlinVersion = '1.4.21'
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenLocal()
|
mavenLocal()
|
||||||
|
@ -8,4 +8,5 @@ dependencies {
|
|||||||
// 计算对象大小
|
// 计算对象大小
|
||||||
api 'org.apache.lucene:lucene-core:4.0.0'
|
api 'org.apache.lucene:lucene-core:4.0.0'
|
||||||
api group: "io.netty", name: "netty-all", version: "4.1.43.Final"
|
api group: "io.netty", name: "netty-all", version: "4.1.43.Final"
|
||||||
|
api group: "io.netty", name: "netty-all", version: "4.1.43.Final"
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,22 @@
|
|||||||
|
package cn.tursom.utils
|
||||||
|
|
||||||
|
import io.netty.buffer.ByteBuf
|
||||||
|
import io.netty.buffer.Unpooled
|
||||||
|
import io.netty.channel.ChannelHandler
|
||||||
|
import io.netty.channel.ChannelHandlerContext
|
||||||
|
import io.netty.channel.ChannelOutboundHandlerAdapter
|
||||||
|
import io.netty.channel.ChannelPromise
|
||||||
|
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame
|
||||||
|
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame
|
||||||
|
|
||||||
|
@ChannelHandler.Sharable
|
||||||
|
object WebSocketFrameWrapper : ChannelOutboundHandlerAdapter() {
|
||||||
|
override fun write(ctx: ChannelHandlerContext, msg: Any?, promise: ChannelPromise?) {
|
||||||
|
ctx.write(when (msg) {
|
||||||
|
is String -> TextWebSocketFrame(msg)
|
||||||
|
is ByteArray -> BinaryWebSocketFrame(Unpooled.wrappedBuffer(msg))
|
||||||
|
is ByteBuf -> BinaryWebSocketFrame(msg)
|
||||||
|
else -> msg
|
||||||
|
}, promise)
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package cn.tursom.ws
|
package cn.tursom.ws
|
||||||
|
|
||||||
import cn.tursom.core.buffer.ByteBuffer
|
import cn.tursom.core.buffer.ByteBuffer
|
||||||
|
import cn.tursom.utils.WebSocketFrameWrapper
|
||||||
import cn.tursom.utils.bytebuffer.NettyByteBuffer
|
import cn.tursom.utils.bytebuffer.NettyByteBuffer
|
||||||
import io.netty.bootstrap.Bootstrap
|
import io.netty.bootstrap.Bootstrap
|
||||||
import io.netty.buffer.ByteBuf
|
import io.netty.buffer.ByteBuf
|
||||||
@ -22,7 +23,7 @@ import io.netty.handler.ssl.util.InsecureTrustManagerFactory
|
|||||||
import java.net.URI
|
import java.net.URI
|
||||||
|
|
||||||
|
|
||||||
class WebSocketClient(uri: String, val handler: WebSocketHandler) {
|
class WebSocketClient(uri: String, val handler: WebSocketHandler, val autoWrap: Boolean = true) {
|
||||||
private val uri: URI = URI.create(uri)
|
private val uri: URI = URI.create(uri)
|
||||||
internal var ch: Channel? = null
|
internal var ch: Channel? = null
|
||||||
|
|
||||||
@ -59,24 +60,27 @@ class WebSocketClient(uri: String, val handler: WebSocketHandler) {
|
|||||||
uri, WebSocketVersion.V13, null, false, DefaultHttpHeaders()
|
uri, WebSocketVersion.V13, null, false, DefaultHttpHeaders()
|
||||||
), this, handler
|
), this, handler
|
||||||
)
|
)
|
||||||
val b = Bootstrap()
|
val bootstrap = Bootstrap()
|
||||||
b.group(group)
|
bootstrap.group(group)
|
||||||
.channel(NioSocketChannel::class.java)
|
.channel(NioSocketChannel::class.java)
|
||||||
.handler(object : ChannelInitializer<SocketChannel>() {
|
.handler(object : ChannelInitializer<SocketChannel>() {
|
||||||
override fun initChannel(ch: SocketChannel) {
|
override fun initChannel(ch: SocketChannel) {
|
||||||
val p = ch.pipeline()
|
val pipeline = ch.pipeline()
|
||||||
if (sslCtx != null) {
|
if (sslCtx != null) {
|
||||||
p.addLast(sslCtx.newHandler(ch.alloc(), host, port))
|
pipeline.addLast(sslCtx.newHandler(ch.alloc(), host, port))
|
||||||
}
|
}
|
||||||
p.addLast(
|
pipeline.addLast(
|
||||||
HttpClientCodec(),
|
HttpClientCodec(),
|
||||||
HttpObjectAggregator(4096),
|
HttpObjectAggregator(4096),
|
||||||
WebSocketClientCompressionHandler.INSTANCE,
|
WebSocketClientCompressionHandler.INSTANCE,
|
||||||
handler
|
handler,
|
||||||
)
|
)
|
||||||
|
if (autoWrap) {
|
||||||
|
pipeline.addLast(WebSocketFrameWrapper)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
b.connect(uri.host, port)
|
bootstrap.connect(uri.host, port)
|
||||||
//handler.handshakeFuture().sync()
|
//handler.handshakeFuture().sync()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package cn.tursom.web.netty
|
package cn.tursom.web.netty
|
||||||
|
|
||||||
|
import cn.tursom.utils.WebSocketFrameWrapper
|
||||||
import cn.tursom.web.HttpHandler
|
import cn.tursom.web.HttpHandler
|
||||||
import cn.tursom.web.HttpServer
|
import cn.tursom.web.HttpServer
|
||||||
import cn.tursom.web.WebSocketHandler
|
import cn.tursom.web.WebSocketHandler
|
||||||
@ -31,6 +32,7 @@ class NettyHttpServer(
|
|||||||
var writeTimeout: Int? = null,
|
var writeTimeout: Int? = null,
|
||||||
decodeType: NettyHttpDecodeType = if (webSocketPath.iterator().hasNext()) NettyHttpDecodeType.FULL_HTTP else NettyHttpDecodeType.MULTI_PART,
|
decodeType: NettyHttpDecodeType = if (webSocketPath.iterator().hasNext()) NettyHttpDecodeType.FULL_HTTP else NettyHttpDecodeType.MULTI_PART,
|
||||||
backlog: Int = 1024,
|
backlog: Int = 1024,
|
||||||
|
val wrapWebSocketFrame: Boolean = false,
|
||||||
) : HttpServer {
|
) : HttpServer {
|
||||||
constructor(
|
constructor(
|
||||||
port: Int,
|
port: Int,
|
||||||
@ -40,13 +42,15 @@ class NettyHttpServer(
|
|||||||
readTimeout: Int? = 60,
|
readTimeout: Int? = 60,
|
||||||
writeTimeout: Int? = null,
|
writeTimeout: Int? = null,
|
||||||
decodeType: NettyHttpDecodeType = if (webSocketPath.iterator().hasNext()) NettyHttpDecodeType.FULL_HTTP else NettyHttpDecodeType.MULTI_PART,
|
decodeType: NettyHttpDecodeType = if (webSocketPath.iterator().hasNext()) NettyHttpDecodeType.FULL_HTTP else NettyHttpDecodeType.MULTI_PART,
|
||||||
|
backlog: Int = 1024,
|
||||||
|
wrapWebSocketFrame: Boolean = false,
|
||||||
handler: (content: NettyHttpContent) -> Unit,
|
handler: (content: NettyHttpContent) -> Unit,
|
||||||
) : this(
|
) : this(
|
||||||
port,
|
port,
|
||||||
object : HttpHandler<NettyHttpContent, NettyExceptionContent> {
|
object : HttpHandler<NettyHttpContent, NettyExceptionContent> {
|
||||||
override fun handle(content: NettyHttpContent) = handler(content)
|
override fun handle(content: NettyHttpContent) = handler(content)
|
||||||
},
|
},
|
||||||
bodySize, autoRun, webSocketPath, readTimeout, writeTimeout, decodeType
|
bodySize, autoRun, webSocketPath, readTimeout, writeTimeout, decodeType, backlog, wrapWebSocketFrame
|
||||||
)
|
)
|
||||||
|
|
||||||
var decodeType: NettyHttpDecodeType = decodeType
|
var decodeType: NettyHttpDecodeType = decodeType
|
||||||
@ -84,6 +88,9 @@ class NettyHttpServer(
|
|||||||
pipeline.addLast("ws-$webSocketPath", WebSocketServerProtocolHandler(webSocketPath))
|
pipeline.addLast("ws-$webSocketPath", WebSocketServerProtocolHandler(webSocketPath))
|
||||||
pipeline.addLast("wsHandler-$webSocketPath", NettyWebSocketHandler(ch, handler))
|
pipeline.addLast("wsHandler-$webSocketPath", NettyWebSocketHandler(ch, handler))
|
||||||
}
|
}
|
||||||
|
if (wrapWebSocketFrame && webSocketPath.iterator().hasNext()) {
|
||||||
|
pipeline.addLast(WebSocketFrameWrapper)
|
||||||
|
}
|
||||||
pipeline.addLast("handle", httpHandler)
|
pipeline.addLast("handle", httpHandler)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user