Search in sources :

Example 1 with ChannelPush

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

the class Service method onReceiveChannelMessage.

public void onReceiveChannelMessage(ChannelHandlerContext ctx, ChannelMessage message) {
    ChannelResponseCallback callback = (ChannelResponseCallback) seq2Callback.get(message.getSeq());
    logger.debug("收到消息 seq:{}", message.getSeq());
    if (message.getType() == 0x20) {
        // 链上链下请求
        logger.debug("channel请求消息 PUSH");
        if (callback != null) {
            // 清空callback再处理
            logger.debug("seq已存在,清除:{}", message.getSeq());
            seq2Callback.remove(message.getSeq());
        }
        try {
            ChannelPush push = new ChannelPush();
            if (pushCallback != null) {
                push.setService(this);
                push.setCtx(ctx);
                push.setMessageID(message.getSeq());
                push.setFromNode(message.getFromNode());
                push.setToNode(message.getToNode());
                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() == 0x21) {
        // 链上链下回包
        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 : ChannelPush(org.bcos.channel.dto.ChannelPush) ChannelResponse(org.bcos.channel.dto.ChannelResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

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