洪城义警-正式版后台
zengh
2021-08-12 e654a4570cca83fe0915bc8da14b1d8c2bc92d11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
 * @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));
    }
 
    @Override
    public int inviteVideoCall(String userId,String time,String type) {
        //返回值
        int res = 0;
        Channel channel = NettyConfig.getUserChannelMap().get(userId);
        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type",type);
        jsonObject.put("roomId",time);
        if (channel != null){
            channel.writeAndFlush(new TextWebSocketFrame(String.valueOf(jsonObject)));
            res = 1;
        }
        return res;
    }
}