Administrator
2022-06-16 2ebca060a47784db76369a4aa84f63293da9be33
src/main/java/org/springblade/modules/webscoket/service/impl/PushMsgServiceImpl.java
@@ -1,32 +1,32 @@
package org.springblade.modules.webscoket.service.impl;
import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springblade.modules.nettyServer.NettyConfig;
import org.springblade.modules.webscoket.service.IPushMsgService;
import org.springframework.stereotype.Service;
/**
 * @author lq
 * @date 2020/4/1 11:20
 */
@Service
public class PushMsgServiceImpl implements IPushMsgService {
    @Override
    public void pushMsg(String userId, String msg) {
        Channel channel = NettyConfig.getUserChannelMap().get(userId);
      if (channel != null){
         channel.writeAndFlush(new TextWebSocketFrame(msg));
      }
    }
    @Override
    public void pushMsg(String msg) {
        ChannelGroup group = NettyConfig.getChannelGroup();
        String name = group.name();
        System.out.println("空间大小:"+group.size()+",名字:"+name);
        group.writeAndFlush(new TextWebSocketFrame(msg));
    }
}
//package org.springblade.modules.webscoket.service.impl;
//
//import io.netty.channel.Channel;
//import io.netty.channel.group.ChannelGroup;
//import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
//import org.springblade.modules.nettyServer.NettyConfig;
//import org.springblade.modules.webscoket.service.IPushMsgService;
//import org.springframework.stereotype.Service;
//
///**
// * @author lq
// * @date 2020/4/1 11:20
// */
//@Service
//public class PushMsgServiceImpl implements IPushMsgService {
//    @Override
//    public void pushMsg(String userId, String msg) {
//        Channel channel = NettyConfig.getUserChannelMap().get(userId);
//      if (channel != null){
//         channel.writeAndFlush(new TextWebSocketFrame(msg));
//      }
//
//    }
//
//    @Override
//    public void pushMsg(String msg) {
//        ChannelGroup group = NettyConfig.getChannelGroup();
//        String name = group.name();
//        System.out.println("空间大小:"+group.size()+",名字:"+name);
//        group.writeAndFlush(new TextWebSocketFrame(msg));
//    }
//}