Commit 4b9d8085 by giaogiao

获取用户在线状态(批量);

获取sign(仅测试使用);
parent c236957f
package com.wecloud.im.controller;
import com.wecloud.im.entity.ImApplication;
import com.wecloud.im.param.GetOnlineStatusParam;
import com.wecloud.im.param.add.ImClientDeviceInfoAdd;
import com.wecloud.im.service.ImApplicationService;
import com.wecloud.im.service.ImClientService;
......@@ -9,8 +10,6 @@ import com.wecloud.im.ws.service.MangerChannelService;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.core.validator.groups.Add;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.geekidea.springbootplus.framework.shiro.jwt.JwtToken;
import io.geekidea.springbootplus.framework.shiro.util.JwtUtil;
import io.swagger.annotations.Api;
......@@ -18,12 +17,14 @@ import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
/**
* 终端表 控制器
*
......@@ -49,7 +50,6 @@ public class ImClientController extends BaseController {
* 添加或修改推送设备信息
*/
@PostMapping("/addDeviceInfo")
@OperationLog(name = "添加或修改推送设备信息", type = OperationLogType.ADD)
@ApiOperation(value = "添加或修改推送设备信息(每次请求都会覆盖之前的数据)")
public ApiResult<Boolean> addDeviceInfo(@Validated(Add.class) @RequestBody ImClientDeviceInfoAdd imClientDevice) throws Exception {
boolean flag = imClientService.addDeviceInfo(imClientDevice);
......@@ -58,25 +58,49 @@ public class ImClientController extends BaseController {
/**
* 获取用户
* 获取用户在线状态(批量)
*
* @return true:在线, false 不在线
*/
@GetMapping("/onlineStatus")
@OperationLog(name = "获取用户在线状态", type = OperationLogType.ADD)
@ApiOperation(value = "获取用户在线状态")
ApiResult<ImOnlineStatusVo> getOnlineStatus(String clientId) {
@PostMapping("/onlineStatus")
@ApiOperation(value = "获取用户在线状态(批量)")
ApiResult<List<ImOnlineStatusVo>> getOnlineStatus(@RequestBody GetOnlineStatusParam getOnlineStatusParam) {
// shiro线程中获取当前token
JwtToken curentJwtToken = JwtUtil.getCurentJwtToken();
// 根据appKey查询appid
ImApplication imApplication = imApplicationService.getOneByAppKey(curentJwtToken.getAppKey());
ArrayList<ImOnlineStatusVo> imOnlineStatusVos = new ArrayList<ImOnlineStatusVo>();
for (String clientId : getOnlineStatusParam.getClientIds()) {
boolean onlineStatus = mangerChannelService.getOnlineStatus(imApplication.getAppKey(), clientId);
ImOnlineStatusVo imOnlineStatusVo = new ImOnlineStatusVo();
imOnlineStatusVo.setStatus(onlineStatus);
return ApiResult.ok(imOnlineStatusVo);
imOnlineStatusVo.setStatus(mangerChannelService.getOnlineStatus(imApplication.getAppKey(), clientId));
imOnlineStatusVo.setClientId(clientId);
imOnlineStatusVos.add(imOnlineStatusVo);
}
return ApiResult.ok(imOnlineStatusVos);
}
//
// /**
// * 获取用户在线状态(批量)
// *
// * @return true:在线, false 不在线
// */
// @PostMapping("/getOnlineStatus")
// @OperationLog(name = "获取用户在线状态", type = OperationLogType.ADD)
// @ApiOperation(value = "获取用户在线状态")
// ApiResult<ImOnlineStatusVo> getOnlinesStatus(String clientId) {
// // shiro线程中获取当前token
// JwtToken curentJwtToken = JwtUtil.getCurentJwtToken();
//
// // 根据appKey查询appid
// ImApplication imApplication = imApplicationService.getOneByAppKey(curentJwtToken.getAppKey());
//
// boolean onlineStatus = mangerChannelService.getOnlineStatus(imApplication.getAppKey(), clientId);
// ImOnlineStatusVo imOnlineStatusVo = new ImOnlineStatusVo();
// imOnlineStatusVo.setStatus(onlineStatus);
// return ApiResult.ok(imOnlineStatusVo);
// }
// /**
// * 添加终端表
......
package com.wecloud.im.controller;
import cn.hutool.crypto.digest.MD5;
import com.wecloud.im.param.GetSignParam;
import com.wecloud.im.service.ImClientLoginService;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* sign 控制器
*
* @author wei
* @since 2021-04-27
*/
@Slf4j
@RestController
@RequestMapping("/signDemo")
@Api(value = "sign API", tags = {"获取sign(Demo)"})
public class SignController extends BaseController {
@Autowired
private ImClientLoginService imClientLoginService;
/**
* 根据客户方生成签名字符串 验证通过则下发token
*/
@GetMapping("/get")
@ApiOperation(value = "获取sign(仅测试使用)", notes = "生成签名")
public String verify(@RequestBody GetSignParam getSignParam) throws Exception {
return new MD5().digestHex(getSignParam.getTimestamp() + getSignParam.getClientId() + getSignParam.getAppKey() + getSignParam.getAppSecret());
}
}
......@@ -62,7 +62,7 @@ public class ChannelInboundHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
if (logger.isDebugEnabled()) {
logger.debug("连接的客户端地址:{}", ctx.channel().remoteAddress());
logger.info("连接的客户端地址:{}", ctx.channel().remoteAddress());
}
ctx.writeAndFlush("客户端" + InetAddress.getLocalHost().getHostName() + "成功与服务端建立连接! ");
super.channelActive(ctx);
......
......@@ -42,9 +42,9 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram
* io密集型任务配置尽可能多的线程数量
*/
private final static ExecutorService TASK_THREAD_POOL_EXECUTOR =
new ThreadPoolExecutor(WsConstants.CPU_PROCESSORS * 10, WsConstants.CPU_PROCESSORS * 20,
new ThreadPoolExecutor(WsConstants.CPU_PROCESSORS * 5, WsConstants.CPU_PROCESSORS * 10,
3L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(2048), NAMED_THREAD_FACTORY, new ThreadPoolExecutor.CallerRunsPolicy());
new LinkedBlockingQueue<Runnable>(1024), NAMED_THREAD_FACTORY, new ThreadPoolExecutor.CallerRunsPolicy());
@Override
protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) {
......@@ -70,8 +70,8 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram
private void execute(ChannelHandlerContext ctx, String data) {
// Long userIdByChannel = appUserChannelsService.getUserIdByChannel(ctx);
//
// log.debug("appWS收到" + userIdByChannel + ":" + data + ",channelId:" + ctx.channel().id().asLongText());
log.debug("WS收到:" + data);
// log.info("appWS收到" + userIdByChannel + ":" + data + ",channelId:" + ctx.channel().id().asLongText());
log.info("WS收到:" + data);
readWsData.convertModel(data, ctx);
}
......@@ -86,7 +86,7 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
log.debug("检测到异常exceptionCaught", cause);
log.info("检测到异常exceptionCaught", cause);
// //排除当客户端意外关闭的情况,不是发送指定指令通知服务器退出,就会产生此错误。
// if (ctx.channel().isActive()) {
......@@ -99,8 +99,8 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram
public void handlerAdded(ChannelHandlerContext ctx) {
String userIdByChannel = mangerChannelService.getUserIdByChannel(ctx);
log.debug("uid:" + userIdByChannel + "," + ",channelId:" + ctx.channel().id().asLongText());
log.debug("连接WS成功handlerAdded");
log.info("uid:" + userIdByChannel + "," + ",channelId:" + ctx.channel().id().asLongText());
log.info("连接WS成功handlerAdded");
}
......@@ -112,10 +112,10 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram
*/
@Override
public void channelInactive(ChannelHandlerContext ctx) {
log.debug("客户端不活跃channelInactive");
log.info("客户端不活跃channelInactive");
// Long userIdByChannel = appUserChannelsService.getUserIdByChannel(ctx);
// log.debug("uid:" + userIdByChannel + "," + "不活跃" + ",channelId:" + ctx.channel().id().asLongText());
// log.info("uid:" + userIdByChannel + "," + "不活跃" + ",channelId:" + ctx.channel().id().asLongText());
// mangerChannelService.remove(ctx);
}
......@@ -125,10 +125,10 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram
@Override
public void handlerRemoved(ChannelHandlerContext ctx) {
log.debug("handlerRemoved");
log.info("handlerRemoved");
// Long userIdByChannel = appUserChannelsService.getUserIdByChannel(ctx);
// log.debug("uid:" + userIdByChannel + "," + "handlerRemoved" + ",channelId:" + ctx.channel().id().asLongText());
// log.info("uid:" + userIdByChannel + "," + "handlerRemoved" + ",channelId:" + ctx.channel().id().asLongText());
}
}
package com.wecloud.im.param;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
/**
* sign
*
* @author wei
* @since 2021-04-29
*/
@Data
@ApiModel(value = "GetOnlineStatusParam")
public class GetOnlineStatusParam {
private static final long serialVersionUID = 1L;
@ApiModelProperty("客户端ID")
List<String> clientIds;
}
package com.wecloud.im.param;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* sign
*
* @author wei
* @since 2021-04-29
*/
@Data
@ApiModel(value = "GetSignParam")
public class GetSignParam {
private static final long serialVersionUID = 1L;
@ApiModelProperty("时间戳")
private String timestamp;
@ApiModelProperty("client客户端id")
private String clientId;
@ApiModelProperty("appKey")
private String appKey;
@ApiModelProperty("密钥")
private String appSecret;
}
......@@ -62,7 +62,7 @@ public class ImClientLoginServiceImpl implements ImClientLoginService {
ImApplication imApplication = imApplicationService.getOneByAppKey(imTokenVerify.getAppKey());
if (imApplication == null) {
log.error("imApplication == null,getAppKey:" + imTokenVerify.getAppKey());
log.info("imApplication == null,getAppKey:" + imTokenVerify.getAppKey());
return ApiResult.result(ApiCode.FAIL, null);
}
......@@ -71,10 +71,8 @@ public class ImClientLoginServiceImpl implements ImClientLoginService {
String mySign = new MD5().digestHex(imTokenVerify.getTimestamp() + imTokenVerify.getClientId() + imApplication.getAppKey() + secret);
// 验证签名
if (mySign.equals(imTokenVerify.getSign())) {
log.debug("sign一致" + mySign);
} else {
log.debug("sign不一致" + mySign);
if (!mySign.equals(imTokenVerify.getSign())) {
log.info("sign不一致" + mySign);
return ApiResult.result(ApiCode.FAIL, null);
}
......@@ -86,7 +84,7 @@ public class ImClientLoginServiceImpl implements ImClientLoginService {
.eq(ImClient::getClientId, imTokenVerify.getClientId()));
if (imClient == null) {
log.debug("client不存在,先走注册流程");
log.info("client不存在,先走注册流程");
imClient = new ImClient();
imClient.setId(new Snowflake(1L, 1L).nextId());
......
......@@ -21,4 +21,6 @@ public class ImOnlineStatusVo extends BaseEntity {
private static final long serialVersionUID = 1L;
@ApiModelProperty("true:在线, false 不在线")
private Boolean status;
private String clientId;
}
......@@ -79,7 +79,7 @@ public class FullHttpRequestUtils {
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, copiedBuffer(context, CharsetUtil.UTF_8));
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json;charset=utf-8");
logger.debug("response:\n" + response.toString() + "\ncontext:" + context);
logger.info("response:\n" + response.toString() + "\ncontext:" + context);
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
}
......@@ -197,6 +197,7 @@ spring-boot-plus:
- /,/index.html
# 应用相关
- /imApplication/**
- /signDemo/get
# 多行字符串权限配置
filter-chain-definitions: |
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment