洪城义警-正式版后台
zengh
2022-05-26 4c19ddd36bdfb43b5ef4e8df3c9a98cd4d356d6c
src/main/java/org/springblade/modules/webscoket/service/impl/PushMsgServiceImpl.java
@@ -1,16 +1,12 @@
package org.springblade.modules.webscoket.service.impl;
import com.alibaba.fastjson.JSONObject;
import io.netty.buffer.ByteBuf;
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;
import java.util.HashMap;
import java.util.Map;
/**
 * @author lq
@@ -31,8 +27,11 @@
   public void pushMsg(String msg) {
      ChannelGroup group = NettyConfig.getChannelGroup();
      String name = group.name();
      JSONObject jsonObject = new JSONObject();
      jsonObject.put("xqid", msg);
      jsonObject.put("type", "taks");
      System.out.println("空间大小:" + group.size() + ",名字:" + name);
      group.writeAndFlush(new TextWebSocketFrame(msg));
      group.writeAndFlush(new TextWebSocketFrame(String.valueOf(jsonObject)));
   }
   @Override
@@ -53,16 +52,22 @@
   }
   @Override
   public int startTask(String serid, String rid,String type) {
   public int startTask(String serid, String rid,String type,String data) {
      //返回值
      int res = 0;
      Channel channel = NettyConfig.getUserChannelMap().get(serid);
      JSONObject jsonObject = new JSONObject();
      jsonObject.put("type", type);
      jsonObject.put("rid", rid);
      if (channel != null) {
         channel.writeAndFlush(new TextWebSocketFrame(String.valueOf(jsonObject)));
         res = 1;
      String[] list = serid.split(",");
      for (int i = 0; i <list.length ; i++) {
         if (!list[i].equals("")){
            Channel channel = NettyConfig.getUserChannelMap().get(list[i]);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("type", type);
            jsonObject.put("rid", rid);
            jsonObject.put("data", data);
            if (channel != null) {
               channel.writeAndFlush(new TextWebSocketFrame(String.valueOf(jsonObject)));
               res = 1;
            }
         }
      }
      return res;
   }