從一個機器上讀資料,比如車速XX,位置XX,用netty獲取,然后傳給其他模塊(非java語言寫)
主要的代碼:
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ReadTimeoutHandler(90));
ch.pipeline().addLast(new DelimiterBasedFrameDecoder(204800, Unpooled.copiedBuffer(new byte[]{0x7e})));
ch.pipeline().addLast(new ServerChannelHandlerAdapter());
}
});
在class ServerChannelHandlerAdapter extends ChannelInboundHandlerAdapter 中的 public void channelRead(ChannelHandlerContext ctx, Object msg) 方法里面,有一段陳述句
for (ByteBuf byteBuf : byteBufs) {
ctx.writeAndFlush(byteBuf);
}
1. 車速,位置這些資料應該是被存在byteBuf里面,但是怎么獲取這里面的資料?
2. 有個python寫的模塊,python里好像沒有netty,里面的東西能被python讀取嗎?
著急找結果,只看了netty1個小時,來不及學了,問下前輩,講的稍微白話一點,謝謝。
uj5u.com熱心網友回復:
int readable = byteBuf.readableBytes();
byte[] msg = new byte[readable];
int index = byteBuf.readerIndex();
in.getBytes(index, msg);
msg就是netty獲取到的內容,現在是byte型別,轉成你需要的型別就可以
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/203742.html
標籤:Web 開發
上一篇:JSP
