洪城义警-正式版后台
zengh
2021-08-13 ccc6e51f22bf0b2b0cebb84e32f55a5cebdb9692
问题修复
4 files modified
133 ■■■■■ changed files
src/main/java/org/springblade/modules/webscoket/WebSocketHandler.java 1 ●●●● patch | view | raw | blame | history
src/main/java/org/springblade/modules/webscoket/controller/PushMsgController.java 45 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/modules/webscoket/service/IPushMsgService.java 33 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/modules/webscoket/service/impl/PushMsgServiceImpl.java 54 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/modules/webscoket/WebSocketHandler.java
@@ -62,6 +62,7 @@
        System.out.println("客户端断开连接:" + ctx.channel());
        //用户离线状态
        String name = ChannelSupervise.findName(ctx.channel().id().asShortText());
        System.out.println(name);
        if ( name != null &&!name.equals("ping") ){
            NettyConfig.getChannelGroup().remove(ctx.channel());
            removeUserId(ctx);
src/main/java/org/springblade/modules/webscoket/controller/PushMsgController.java
@@ -20,33 +20,40 @@
@RequestMapping("pushMsg")
public class PushMsgController {
    @Autowired
    private IPushMsgService pushMsgService;
    @Autowired
    private IPushMsgService pushMsgService;
    @PostMapping("/pushUser")
    public String pushUser(String userId,String msg){
        pushMsgService.pushMsg(userId, msg);
        return "消息发送成功:"+msg;
    }
    @PostMapping("/pushUser")
    public String pushUser(String userId, String msg) {
        pushMsgService.pushMsg(userId, msg);
        return "消息发送成功:" + msg;
    }
    @PostMapping("/pushAll")
    public String pushAll(String msg){
        pushMsgService.pushMsg(msg);
        return "消息发送成功:"+msg;
    }
    @PostMapping("/pushAll")
    public String pushAll(String msg) {
        pushMsgService.pushMsg(msg);
        return "消息发送成功:" + msg;
    }
    @GetMapping("/inviteVideoCall")
    public R<Map> inviteVideoCall(String userId,String type){
        //获取当前时间戳作为房间号
    @GetMapping("/inviteVideoCall")
    public R<Map> inviteVideoCall(String userId, String type, String name,String faqiid) {
        //获取当前时间戳作为房间号
        String roomId = "";
        Map<String, Object> map = new HashMap<String, Object>();
        String time = String.valueOf(new Date().getTime());
        int msg = pushMsgService.inviteVideoCall(userId,time,type);
        map.put("type",type);
        map.put("roomId",time);
        map.put("res",0);
        int msg = pushMsgService.inviteVideoCall(userId, time, type, name,faqiid);
        map.put("type", type);
        map.put("roomId", time);
        map.put("res", msg);
        return R.data(map);
    }
    @GetMapping("/closeVideoCall")
    public void closeVideoCall(String sentId, String acceptId) {
        pushMsgService.closeVideoCall(sentId, acceptId);
    }
}
src/main/java/org/springblade/modules/webscoket/service/IPushMsgService.java
@@ -5,22 +5,31 @@
 */
public interface IPushMsgService {
    /**
     * 给指定用户发送消息
     * @param userId
     * @param msg
     */
    void pushMsg(String userId,String msg);
    /**
     * 给指定用户发送消息
     *
     * @param userId
     * @param msg
     */
    void pushMsg(String userId, String msg);
    /**
     * 给所有用户发送消息
     * @param msg
     */
    void pushMsg(String msg);
    /**
     * 给所有用户发送消息
     *
     * @param msg
     */
    void pushMsg(String msg);
    /**
     * 给指定用户发起视频邀请
     */
    int inviteVideoCall(String userid,String time,String type);
    int inviteVideoCall(String userid, String time, String type, String name,String faqiid);
    /**
     * 关闭视频请求
     *
     * @return
     */
    void closeVideoCall(String sentId, String acceptId);
}
src/main/java/org/springblade/modules/webscoket/service/impl/PushMsgServiceImpl.java
@@ -18,36 +18,52 @@
 */
@Service
public class PushMsgServiceImpl implements IPushMsgService {
    @Override
    public void pushMsg(String userId, String msg) {
        Channel channel = NettyConfig.getUserChannelMap().get(userId);
        if (channel != null){
    @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 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) {
        //返回值
    @Override
    public int inviteVideoCall(String userId, String time, String type, String name,String faqiid) {
        //返回值
        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){
        jsonObject.put("type", type);
        jsonObject.put("roomId", time);
        jsonObject.put("name", name);
        jsonObject.put("faqiid", faqiid);
        if (channel != null) {
            channel.writeAndFlush(new TextWebSocketFrame(String.valueOf(jsonObject)));
            res = 1;
        }
        return res;
    }
    @Override
    public void closeVideoCall(String sentId, String acceptId) {
        Channel channel1 = NettyConfig.getUserChannelMap().get(sentId);
        Channel channel2 = NettyConfig.getUserChannelMap().get(acceptId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "close");
        if (channel1 != null) {
            channel1.writeAndFlush(new TextWebSocketFrame(String.valueOf(jsonObject)));
        }
        if (channel2 != null) {
            channel2.writeAndFlush(new TextWebSocketFrame(String.valueOf(jsonObject)));
        }
    }
}