Search in sources :

Example 1 with ChannelPush2

use of org.bcos.channel.dto.ChannelPush2 in project web3sdk by FISCO-BCOS.

the class Service method onReceiveChannelMessage2.

public void onReceiveChannelMessage2(ChannelHandlerContext ctx, ChannelMessage2 message) {
    ChannelResponseCallback2 callback = (ChannelResponseCallback2) seq2Callback.get(message.getSeq());
    logger.debug("收到消息 seq:{}", message.getSeq());
    if (message.getType() == 0x30) {
        // 链上链下请求
        logger.debug("channel请求消息 PUSH");
        if (callback != null) {
            // 清空callback再处理
            logger.debug("seq已存在,清除:{}", message.getSeq());
            seq2Callback.remove(message.getSeq());
        }
        try {
            ChannelPush2 push = new ChannelPush2();
            if (pushCallback != null) {
                // pushCallback.setInfo(info);
                push.setSeq(message.getSeq());
                push.setService(this);
                push.setCtx(ctx);
                push.setTopic(message.getTopic());
                push.setSeq(message.getSeq());
                push.setMessageID(message.getSeq());
                push.setContent(new String(message.getData(), 0, message.getData().length));
                pushCallback.onPush(push);
            } else {
                logger.error("无法push消息,未设置push callback");
            }
        } catch (Exception e) {
            logger.error("处理PUSH消息失败:", e);
        }
    } else if (message.getType() == 0x31) {
        // 链上链下回包
        logger.debug("channel回包消息:{}", message.getSeq());
        if (callback != null) {
            logger.debug("已找到callback 回包消息");
            ChannelResponse response = new ChannelResponse();
            if (message.getResult() != 0) {
                response.setErrorCode(message.getResult());
                response.setErrorMessage("回包错误");
            }
            response.setErrorCode(message.getResult());
            response.setMessageID(message.getSeq());
            if (message.getData() != null) {
                response.setContent(new String(message.getData()));
            }
            callback.onResponse(response);
        } else {
            logger.error("未找到回包callback,可能已超时:{}", message.getData());
            return;
        }
    }
}
Also used : ChannelPush2(org.bcos.channel.dto.ChannelPush2) ChannelResponse(org.bcos.channel.dto.ChannelResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ChannelPush2 (org.bcos.channel.dto.ChannelPush2)1 ChannelResponse (org.bcos.channel.dto.ChannelResponse)1