| | |
| | | import io.netty.channel.ChannelInboundHandlerAdapter |
| | | import io.netty.util.AttributeKey |
| | | |
| | | |
| | | class ServerHandler : ChannelInboundHandlerAdapter() { |
| | | |
| | | val attributeKey = AttributeKey.valueOf<String>("deviceCode") |
| | |
| | | |
| | | override fun channelRead(ctx: ChannelHandlerContext?, msg: Any?) { |
| | | super.channelRead(ctx, msg) |
| | | val attribute = ctx?.channel()?.attr(attributeKey)?.apply { |
| | | if (get() == null) { |
| | | // set() |
| | | } |
| | | println("------收到的原始数据:[ip:${ctx?.channel()?.remoteAddress()}]\r\n$msg") |
| | | if (msg is String) { |
| | | MessageManager().dealMsg(msg, ctx) |
| | | } |
| | | |
| | | // val attribute = ctx?.channel()?.attr(attributeKey)?.apply { |
| | | // if (get() == null) { |
| | | // set(data.obdVin) |
| | | // } |
| | | // } |
| | | |
| | | } |
| | | |
| | | override fun channelReadComplete(ctx: ChannelHandlerContext?) { |
| | |
| | | } |
| | | |
| | | override fun exceptionCaught(ctx: ChannelHandlerContext?, cause: Throwable?) { |
| | | super.exceptionCaught(ctx, cause) |
| | | cause?.printStackTrace() |
| | | ctx?.close() |
| | | } |
| | | } |