src/main/java/org/springblade/Application.java
@@ -16,11 +16,11 @@ */ package org.springblade; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; import org.springblade.common.constant.CommonConstant; import org.springblade.core.launch.BladeApplication; import org.springblade.modules.webscoket.WebSocketServer; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.scheduling.annotation.EnableScheduling; /** @@ -30,11 +30,16 @@ */ @EnableScheduling @SpringBootApplication public class Application { public class Application implements CommandLineRunner { public static void main(String[] args) { BladeApplication.run(CommonConstant.APPLICATION_NAME, Application.class, args); } @Override public void run(String... args) throws Exception { WebSocketServer webSocketServer= new WebSocketServer(9034); } } src/main/java/org/springblade/modules/webscoket/WebSocketHandler.java
@@ -1,5 +1,7 @@ package org.springblade.modules.webscoket; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFuture; @@ -61,12 +63,6 @@ //用户离线状态 String name = ChannelSupervise.findName(ctx.channel().id().asShortText()); if ( name != null &&!name.equals("ping") ){ String num="0"; //工作状态(0闲置,1工作中) String workSt = "0"; webSocketHandler.suserService.updateUser(num,name,workSt); //ChannelSupervise.removeChannel(ctx.channel()); NettyConfig.getChannelGroup().remove(ctx.channel()); removeUserId(ctx); } @@ -100,21 +96,29 @@ if (!request.equals("ping")){ NettyConfig.getUserChannelMap().put(request,ctx.channel()); JSONObject jsonObj = JSON.parseObject(request); String type = jsonObj.get("type").toString(); //将用户id作为自定义属性加入到channel 中,方便随时channel中获取用户id AttributeKey<String> key = AttributeKey.valueOf("userId"); ctx.channel().attr(key).setIfAbsent(request); if (type != null && type.equals("login")){ //登录链接 String id = jsonObj.get("id").toString(); NettyConfig.getUserChannelMap().put(id,ctx.channel()); //把用户信息添加到通道里 ChannelSupervise.addChannel(ctx.channel(),request); //用户在线状态 this.on=request; //在线状态(0掉线,1在线) String num="1"; //工作状态(0闲置,1工作中) String workSt = "0"; webSocketHandler.suserService.updateUser(num,request,workSt); System.out.println(jsonObj.get("type")); System.out.println(jsonObj.get("id")); //将用户id作为自定义属性加入到channel 中,方便随时channel中获取用户id AttributeKey<String> key = AttributeKey.valueOf("userId"); ctx.channel().attr(key).setIfAbsent(id); //把用户信息添加到通道里 ChannelSupervise.addChannel(ctx.channel(),id); } } // TextWebSocketFrame tws = new TextWebSocketFrame(new Date().toString() src/main/java/org/springblade/modules/webscoket/controller/PushMsgController.java
@@ -1,15 +1,23 @@ package org.springblade.modules.webscoket.controller; import org.springblade.core.tool.api.R; import org.springblade.modules.webscoket.service.IPushMsgService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * @author lq * @date 2020/4/1 11:22 */ @RestController @RequestMapping("pushMsg") public class PushMsgController { @Autowired @@ -27,4 +35,18 @@ return "消息发送成功:"+msg; } @GetMapping("/inviteVideoCall") public R<Map> inviteVideoCall(String userId,String type){ //获取当前时间戳作为房间号 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); return R.data(map); } } src/main/java/org/springblade/modules/webscoket/service/IPushMsgService.java
@@ -18,4 +18,9 @@ */ void pushMsg(String msg); /** * 给指定用户发起视频邀请 */ int inviteVideoCall(String userid,String time,String type); } src/main/java/org/springblade/modules/webscoket/service/impl/PushMsgServiceImpl.java
@@ -1,11 +1,16 @@ 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 @@ -29,4 +34,20 @@ 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; } } src/main/resources/application-dev.yml
@@ -17,6 +17,10 @@ # username: root # password: jfpt123 # url: jdbc:mysql://localhost:2083/qfqkpublic?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true # username: root # password: zhba0728 url: jdbc:mysql://223.82.109.183:2083/qfqkpublic?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true username: root password: zhba0728