src/main/kotlin/com/flightfeather/obd/socket/SocketServerClient.kt
@@ -7,6 +7,7 @@
import io.netty.channel.socket.SocketChannel
import io.netty.channel.socket.nio.NioServerSocketChannel
import io.netty.channel.socket.nio.NioSocketChannel
import io.netty.handler.codec.string.StringDecoder
/**
 * socket长连接服务端
@@ -15,16 +16,19 @@
 */
class SocketServerClient {
//    val sessionMap = HashMap<String, IoSession>
    private val bossGroup = NioEventLoopGroup()
    private val workerGroup = NioEventLoopGroup()
    fun startServer(port: Int) {
        initialize()?.bind(port)?.sync()
                ?.channel()?.closeFuture()?.sync()
    }
    fun stopServer() {
        bossGroup.shutdownGracefully()
        workerGroup.shutdownGracefully()
    }
    private fun initialize(): ServerBootstrap? {
        val bossGroup = NioEventLoopGroup()
        val workerGroup = NioEventLoopGroup()
        try {
            return ServerBootstrap()
@@ -32,16 +36,15 @@
                    .channel(NioServerSocketChannel::class.java)
                    .childHandler(object : ChannelInitializer<NioSocketChannel>() {
                        override fun initChannel(p0: NioSocketChannel?) {
                            p0?.pipeline()?.addLast(ServerHandler())
                            p0?.pipeline()
                                    ?.addLast(StringDecoder())
                                    ?.addLast(ServerHandler())
                        }
                    })
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true)
        } catch (e: Throwable) {
            e.printStackTrace()
        } finally {
            bossGroup.shutdownGracefully()
            workerGroup.shutdownGracefully()
        }
        return null