| | |
| | | package com.flightfeather.uav.socket.handler |
| | | |
| | | import com.flightfeather.uav.biz.sourcetrace.SourceTraceController |
| | | import com.flightfeather.uav.common.utils.GsonUtils |
| | | import com.flightfeather.uav.domain.entity.BaseRealTimeData |
| | | import com.flightfeather.uav.domain.repository.SceneInfoRep |
| | | import com.flightfeather.uav.lightshare.bean.DataVo |
| | | import com.flightfeather.uav.socket.sender.UnderwayWebSocketSender |
| | | import io.netty.channel.ChannelHandlerContext |
| | | import io.netty.handler.codec.http.websocketx.TextWebSocketFrame |
| | | import org.springframework.stereotype.Component |
| | | |
| | | /** |
| | | * |
| | | * @date 2025/5/13 |
| | | * @author feiyu02 |
| | | */ |
| | | @Component |
| | | class UnderwayWebSocketServerHandler : BaseHandler() { |
| | | class UnderwayWebSocketServerHandler(sceneInfoRep: SceneInfoRep) : BaseHandler() { |
| | | |
| | | private val sessionPool = mutableMapOf<String?, ChannelHandlerContext?>() |
| | | private val sourceTraceController = SourceTraceController(sceneInfoRep) |
| | | |
| | | override var tag: String = "UAV-WS" |
| | | |
| | | override fun channelRegistered(ctx: ChannelHandlerContext?) { |
| | | super.channelRegistered(ctx) |
| | | // 将连接存储 |
| | | if (!sessionPool.containsKey(ctx?.name())) { |
| | | sessionPool[ctx?.name()] = ctx |
| | | } |
| | | UnderwayWebSocketSender.saveSession(ctx) |
| | | } |
| | | |
| | | override fun channelRead(ctx: ChannelHandlerContext?, msg: Any?) { |
| | | super.channelRead(ctx, msg) |
| | | |
| | | when (msg) { |
| | | is TextWebSocketFrame->{ |
| | | println(msg.text()) |
| | | ctx?.channel()?.writeAndFlush(msg) |
| | | is TextWebSocketFrame -> { |
| | | val msgTxt = msg.text() |
| | | |
| | | println(msgTxt) |
| | | // ctx?.channel()?.writeAndFlush(msg) |
| | | |
| | | // Test |
| | | try { |
| | | if (msgTxt == "start") { |
| | | sourceTraceController.initTask() |
| | | } else { |
| | | val data = GsonUtils.parserJsonToArrayBeans(msgTxt, DataVo::class.java) |
| | | data.forEach { |
| | | sourceTraceController.addOneData( |
| | | it.toBaseRealTimeData(BaseRealTimeData::class.java) |
| | | ) |
| | | } |
| | | } |
| | | } catch (e: Exception) { |
| | | // ctx?.channel()?.writeAndFlush(TextWebSocketFrame("当前为测试状态,传输的数据不是走航数据格式")) |
| | | println("当前为测试状态,传输的数据不是走航数据格式") |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | override fun channelInactive(ctx: ChannelHandlerContext?) { |
| | | super.channelInactive(ctx) |
| | | // 将连接移除 |
| | | if (sessionPool.containsKey(ctx?.name())) { |
| | | sessionPool.remove(ctx?.name()) |
| | | } |
| | | } |
| | | |
| | | fun send() { |
| | | |
| | | } |
| | | |
| | | fun broadcast(msg: String) { |
| | | sessionPool.forEach { t, u -> |
| | | u?.channel()?.writeAndFlush(TextWebSocketFrame(msg)) |
| | | } |
| | | UnderwayWebSocketSender.removeSession(ctx) |
| | | } |
| | | } |