| | |
| | | import io.netty.channel.ChannelHandlerContext; |
| | | import io.netty.channel.ChannelInboundHandlerAdapter; |
| | | import android.util.Log; |
| | | |
| | | import com.example.agvcontroller.Item; |
| | | |
| | | |
| | | import org.greenrobot.eventbus.EventBus; |
| | | import org.greenrobot.eventbus.Subscribe; |
| | | import org.greenrobot.eventbus.ThreadMode; |
| | | |
| | | import java.util.Arrays; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | |
| | | public class NettyServerHandler extends ChannelInboundHandlerAdapter { |
| | |
| | | public void channelActive(ChannelHandlerContext ctx) throws Exception { |
| | | String clientId = ctx.channel().remoteAddress().toString(); |
| | | channelMap.put(clientId, ctx.channel()); |
| | | EventBus.getDefault().post(new Item("1",clientId,"3")); |
| | | Log.d(TAG, "Client connected: " + clientId); |
| | | } |
| | | |
| | |
| | | public void channelInactive(ChannelHandlerContext ctx) throws Exception { |
| | | String clientId = ctx.channel().remoteAddress().toString(); |
| | | channelMap.remove(clientId); |
| | | EventBus.getDefault().post(clientId); |
| | | Log.d(TAG, "Client disconnected: " + clientId); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | public static void sendMessageToClient(String clientId, byte[] message) { |
| | | |
| | | Channel channel = channelMap.get(clientId); |
| | | if (channel != null && channel.isActive()) { |
| | | ByteBuf buf = Unpooled.wrappedBuffer(message); |
| | |
| | | } else { |
| | | Log.d(TAG, "Client " + clientId + " is not connected"); |
| | | } |
| | | |
| | | Log.d(TAG, "Client " + clientId + " is not connected"); |
| | | } |
| | | |
| | | @Override |