亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據輸入消息將結果作為 StringEncoder 或 ObjectEncoder 發送到客戶端?

根據輸入消息將結果作為 StringEncoder 或 ObjectEncoder 發送到客戶端?

滄海一幻覺 2021-12-01 19:57:34
我是 Netty 的新手,到目前為止,有一個可以使用 StringEncoder 和 StringDecoder 處理字符串消息的工作客戶端服務器,例如:        ServerBootstrap serverBootstrap = new ServerBootstrap();        serverBootstrap.group(bossGroup, workerGroup)                       .channel(NioServerSocketChannel.class)                       .handler(new LoggingHandler(LogLevel.TRACE))                       .childHandler(new ChannelInitializer<SocketChannel>() {                                            @Override                                            public void initChannel(SocketChannel ch) throws Exception {                                                ch.pipeline().addLast(                                                    new LoggingHandler(LogLevel.TRACE),                                                    new DelimiterBasedFrameDecoder(Integer.MAX_VALUE, Delimiters.lineDelimiter()),                                                    new StringEncoder(),                                                    new StringDecoder(),                                                    new ReceiveMessageCommunicatorHandler(localNode, inpeerNodeRegistry, outpeerNodeRegistry));輸入消息就像這樣:excute_this和服務器發回:ok?,F在,我需要做的是服務器收到一條新的文本消息:get_object,它應該將序列化對象作為字節數組發送回任何客戶端(例如:telnet 或使用普通套接字的 java 應用程序)。然后在這個客戶端中,它可以將這個對象反序列化為 Java 對象。我知道ObjectEncoder()似乎是這樣做的方法。但是,它還將應該是字符串的響應序列化為序列化對象,而它應該為某些特定消息(例如:僅get_object)執行此操作。我怎么能告訴 Netty 什么時候應該將結果編碼為 StringEncoder,什么時候應該將序列化對象編碼為字節數組?
查看完整描述

2 回答

?
慕娘9325324

TA貢獻1783條經驗 獲得超5個贊

可以動態修改管道。當您收到get_object消息時,您可以簡單地刪除StringEncoder并ObjectEncoder在相關的ChannelInboundHandler


   ChannelPipeline p = ctx.pipeline();

    if (p.get(StringEncoder.class) != null) {

        p.remove(StringEncoder.class);

    }

    p.addLast(new YourObjectEncoder())

或者,如果您知道編碼器的名稱,則可以進行替換:


p.replace("encoder", "encoder", new YourObjectEncoder());


查看完整回答
反對 回復 2021-12-01
?
阿波羅的戰車

TA貢獻1862條經驗 獲得超6個贊

當服務器收到消息并將響應寫入客戶端時,它可以像這段代碼一樣動態切換編碼器。


@Override

protected void channelRead0(ChannelHandlerContext ctx, String message) throws Exception {

    InetSocketAddress address = (InetSocketAddress) ctx.channel().remoteAddress();

    MessageContainer messageContainer = new MessageContainer(message, address);

    log.debug("\n");

    log.debug("Message received: {}", message);


    // Handle received message and write result back to the sender

    Object response = this.handleMessage(messageContainer);

    if (response instanceof String) {

        ctx.channel().pipeline().names();

        ctx.channel().pipeline().remove(ObjectEncoder.class);

        ctx.channel().pipeline().addFirst(new StringEncoder());

    }


    if (response != null) {

        ctx.write(response);

        ctx.flush();

    }

    ctx.close();

}


查看完整回答
反對 回復 2021-12-01
  • 2 回答
  • 0 關注
  • 250 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號