package com.mx.cneeds.server.wechat.common.codec;

import com.alibaba.fastjson.JSON;
import com.mx.cneeds.server.wechat.websocket.config.NettyChannelManager;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @ClassName MessageToWebSocketEncoder
 * @Author zzrdark
 * @Date 2020-06-23 21:28
 * @Description TODO
 **/
@Slf4j
public class MessageToWebSocketEncoder extends MessageToMessageEncoder<Invocation> {
    @Override
    protected void encode(ChannelHandlerContext ctx, Invocation invocation, List<Object> list) throws Exception {
//        String toJsonString = JSON.toJSONString(invocation.getMessage());
        log.debug("用户：["+ctx.channel().attr(NettyChannelManager.CHANNEL_ATTR_KEY_USER).get() + "]发出一条消息({})",invocation.getMessage());
        list.add(new TextWebSocketFrame(invocation.getMessage()));
    }
}
