Commit 1f4b369f by 罗志长

fix: rtm token获取

parent b674619f
......@@ -145,7 +145,7 @@
appId: 883078934ecd4193aa7a62a3cdacd810
appCertificate: b29be69c9c034120a68f1d5c199d2e74
channelName: 1
uid: 0
uid: 10010 #RTM用户id唯一,不同应用请自行修改
tokenExpirationInSeconds: 3600
privilegeExpirationInSeconds: 3600
customerKey: b3b5f44e536a4fc191358926c6716b7b
......
......@@ -113,6 +113,10 @@ public class RedisConst {
public static final String RTC_EVENT_CALLBACK_PREFIX = "rtc:event:callback:";
public static final String RTM_RESIDENT_USER_TOKEN_PREFIX = "rtm:resident:user:";
public static final String RTM_RESIDENT_USER_LOGIN_STATUS_PREFIX = "rtm:resident:user:login:";
}
......
......@@ -9,10 +9,6 @@ import javax.validation.constraints.NotBlank;
public class RTMSendPeerMessageDTO {
@NotBlank
@ApiModelProperty(value = "RTM userId")
private String userId;
@NotBlank
@ApiModelProperty(value = "设备id")
private String deviceId;
......@@ -21,10 +17,6 @@ public class RTMSendPeerMessageDTO {
private String rtcToken;
@NotBlank
@ApiModelProperty(value = "RTM token")
private String rtmToken;
@NotBlank
@ApiModelProperty(value = "频道")
private String channel;
......
package com.makeit.shengwang.agora.rtm;
import com.makeit.enums.redis.RedisConst;
import com.makeit.utils.old.StringUtils;
import com.makeit.utils.redis.RedisUtil;
import io.agora.rtm.*;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Slf4j
public class RtmInstance {
......@@ -38,8 +42,11 @@ public class RtmInstance {
log.info("on token expired");
}
// 当前使用的 RTM Token 还有 30 秒过期
@Override
public void onTokenPrivilegeWillExpire() {
RedisUtil.deleteLike(RedisConst.RTM_RESIDENT_USER_TOKEN_PREFIX);
RedisUtil.deleteLike(RedisConst.RTM_RESIDENT_USER_LOGIN_STATUS_PREFIX);
}
@Override
......@@ -70,31 +77,39 @@ public class RtmInstance {
}
public static void sendPeerMessage(String token, String userId, String dst, String message) {
rtmClient.login(token, userId, new ResultCallback<Void>() {
String key = RedisConst.RTM_RESIDENT_USER_LOGIN_STATUS_PREFIX + userId;
String value = RedisUtil.get(key);
if (StringUtils.isBlank(value)) {
rtmClient.login(token, userId, new ResultCallback<Void>() {
@Override
public void onSuccess(Void responseInfo) {
log.info("{} login success!", userId);
RedisUtil.set(key, "1", 1, TimeUnit.HOURS);
sendPeerMessage(dst, message);
}
@Override
public void onFailure(ErrorInfo errorInfo) {
log.info("{} login failure: {}!", userId, errorInfo);
}
});
} else {
sendPeerMessage(dst, message);
}
}
public static void sendPeerMessage(String dst, String message) {
RtmMessage msg = rtmClient.createMessage();
msg.setText(message);
rtmClient.sendMessageToPeer(dst, msg, new ResultCallback<Void>() {
@Override
public void onSuccess(Void responseInfo) {
log.info("{} login success!", userId);
RtmMessage msg = rtmClient.createMessage();
msg.setText(message);
rtmClient.sendMessageToPeer(dst, msg, new ResultCallback<Void>() {
@Override
public void onSuccess(Void aVoid) {
log.info("send message to peer success, message: {}!", message);
logout();
}
@Override
public void onFailure(ErrorInfo errorInfo) {
log.info("send message to peer failed, error info = {}", errorInfo);
logout();
}
});
public void onSuccess(Void aVoid) {
log.info("send message to peer success, message: {}!", message);
}
@Override
public void onFailure(ErrorInfo errorInfo) {
log.info("{} login failure: {}!", userId, errorInfo);
log.info("send message to peer failed, error info = {}", errorInfo);
}
});
}
......
......@@ -6,14 +6,15 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.makeit.config.ShengwangProperties;
import com.makeit.enums.redis.RedisConst;
import com.makeit.shengwang.agora.dto.PlatCallingDeviceDTO;
import com.makeit.shengwang.agora.media.RtcTokenBuilder2;
import com.makeit.shengwang.agora.rtm.RtmTokenBuilder2;
import com.makeit.shengwang.agora.vo.PlatAlarmCallDeviceVO;
import com.makeit.utils.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
......@@ -27,15 +28,12 @@ import java.util.concurrent.TimeUnit;
@Component
public class ShengwangService {
public static final String REDIS_DEVICE_CALL = "device::call";
@Autowired
private ShengwangProperties shengwangProperties;
@Autowired
private StringRedisTemplate redisTemplate;
public PlatAlarmCallDeviceVO callingDeviceAuthIgnoreRtm(PlatCallingDeviceDTO dto) {
String redisResult = redisTemplate.opsForValue().get(REDIS_DEVICE_CALL + dto.getUserId());
String key = RedisConst.RTM_RESIDENT_USER_TOKEN_PREFIX + dto.getUserId();
String redisResult = RedisUtil.get(key);
if (StringUtils.isNotBlank(redisResult)) {
return JSON.parseObject(redisResult,PlatAlarmCallDeviceVO.class);
}
......@@ -49,8 +47,7 @@ public class ShengwangService {
platAlarmCallDeviceVO.setChannelName(format);
platAlarmCallDeviceVO.setAppId(shengwangProperties.getAppId());
platAlarmCallDeviceVO.setUserId(dto.getUserId());
redisTemplate.opsForValue().set(REDIS_DEVICE_CALL + dto.getUserId(),JSON.toJSONString(platAlarmCallDeviceVO),
shengwangProperties.getTokenExpirationInSeconds(), TimeUnit.SECONDS);
RedisUtil.set(key, JSON.toJSONString(platAlarmCallDeviceVO), shengwangProperties.getTokenExpirationInSeconds(), TimeUnit.SECONDS);
return platAlarmCallDeviceVO;
}
......
package com.makeit.startup.runner;
import com.makeit.enums.redis.RedisConst;
import com.makeit.utils.redis.RedisUtil;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class RtmRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
RedisUtil.deleteLike(RedisConst.RTM_RESIDENT_USER_TOKEN_PREFIX);
RedisUtil.deleteLike(RedisConst.RTM_RESIDENT_USER_LOGIN_STATUS_PREFIX);
}
}
......@@ -15,13 +15,13 @@ public class TestRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
logger.info("TestRunner run");
// logger.info("TestRunner run");
}
@Order(1)
@Runner
public void run() throws Exception {
logger.info("TestRunner run 2");
// logger.info("TestRunner run 2");
}
}
......@@ -8,7 +8,6 @@ import com.makeit.common.response.ApiResponseEntity;
import com.makeit.common.response.ApiResponseUtils;
import com.makeit.dto.wechat.device.PlatChildDeviceDTO;
import com.makeit.dto.wechat.device.PlatDeviceSetupDTO;
import com.makeit.global.annotation.AuthIgnore;
import com.makeit.global.aspect.tenant.TenantIdIgnore;
import com.makeit.service.platform.device.PlatDeviceService;
import com.makeit.shengwang.agora.dto.PlatCallingDeviceDTO;
......@@ -69,14 +68,6 @@ public class PlatDeviceChildrenController {
return ApiResponseUtils.success(platDeviceService.callingDevice(dto));
}
@ApiOperation("设备获取登录RTMtoken")
@PostMapping("getDeviceRtmToken")
@AuthIgnore
@TenantIdIgnore
public ApiResponseEntity<PlatAlarmCallDeviceVO> getDeviceRtmToken(@RequestBody PlatCallingDeviceDTO dto) {
return ApiResponseUtils.success(platDeviceService.getDeviceRtmToken(dto));
}
@ApiOperation("发送设备消息")
@PostMapping("sendPeerMessage")
@TenantIdIgnore
......
......@@ -131,14 +131,6 @@ public class PlatDeviceWechatController {
return ApiResponseUtils.success(platDeviceService.callingDevice(dto));
}
@ApiOperation("设备获取登录RTMtoken")
@PostMapping("getDeviceRtmToken")
@AuthIgnore
@TenantIdIgnore
public ApiResponseEntity<PlatAlarmCallDeviceVO> getDeviceRtmToken(@RequestBody PlatCallingDeviceDTO dto) {
return ApiResponseUtils.success(platDeviceService.getDeviceRtmToken(dto));
}
@ApiOperation("发送设备消息")
@PostMapping("sendPeerMessage")
@TenantIdIgnore
......
......@@ -1357,7 +1357,10 @@ public class PlatDeviceServiceImpl extends ServiceImpl<PlatDeviceMapper, PlatDev
data.put("token", dto.getRtcToken());
data.put("channel", dto.getChannel());
params.put("data", data);
RtmInstance.sendPeerMessage(dto.getRtmToken(), dto.getUserId(), dto.getDeviceId(), JSONObject.toJSONString(params));
PlatCallingDeviceDTO callingDeviceDTO = new PlatCallingDeviceDTO();
callingDeviceDTO.setUserId(shengwangProperties.getUid());
PlatAlarmCallDeviceVO rtmToken = this.getDeviceRtmToken(callingDeviceDTO);
RtmInstance.sendPeerMessage(rtmToken.getAccessToken(), rtmToken.getUserId(), dto.getDeviceId(), JSONObject.toJSONString(params));
}
}
......@@ -160,7 +160,7 @@ shengwang:
appId: 883078934ecd4193aa7a62a3cdacd810
appCertificate: b29be69c9c034120a68f1d5c199d2e74
channelName: 1
uid: 0
uid: 10000 #RTM用户id,不同应用请修改
tokenExpirationInSeconds: 3600
privilegeExpirationInSeconds: 3600
customerKey: b3b5f44e536a4fc191358926c6716b7b
......
......@@ -159,7 +159,7 @@ shengwang:
appId: 883078934ecd4193aa7a62a3cdacd810
appCertificate: b29be69c9c034120a68f1d5c199d2e74
channelName: 1
uid: 0
uid: 10001 #RTM用户id,不同应用请修改
tokenExpirationInSeconds: 3600
privilegeExpirationInSeconds: 3600
customerKey: b3b5f44e536a4fc191358926c6716b7b
......
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 sign in to comment