Commit d23c5aa8 by 罗志长

fix: rtm优化

parent adf4f15f
...@@ -5,27 +5,28 @@ import com.makeit.utils.old.StringUtils; ...@@ -5,27 +5,28 @@ import com.makeit.utils.old.StringUtils;
import com.makeit.utils.redis.RedisUtil; import com.makeit.utils.redis.RedisUtil;
import io.agora.rtm.*; import io.agora.rtm.*;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@Slf4j @Slf4j
@Component
public class RtmInstance { public class RtmInstance {
public static final String APP_ID = "883078934ecd4193aa7a62a3cdacd810"; private static String APP_ID;
private static final RtmClient rtmClient; private static String UID;
private static RtmClient RTM_CLIENT;
static {
rtmClient = init();
}
private RtmInstance() { private RtmInstance() {
} }
private static RtmClient init() { @PostConstruct
RtmClient rtmClient = null; private void init() {
try { try {
rtmClient = RtmClient.createInstance(APP_ID, new RtmClientListener() { RTM_CLIENT = RtmClient.createInstance(APP_ID, new RtmClientListener() {
@Override @Override
public void onConnectionStateChanged(int state, int reason) { public void onConnectionStateChanged(int state, int reason) {
log.info("on connection state changed to " + state + " reason: " + reason); log.info("on connection state changed to " + state + " reason: " + reason);
...@@ -45,8 +46,9 @@ public class RtmInstance { ...@@ -45,8 +46,9 @@ public class RtmInstance {
// 当前使用的 RTM Token 还有 30 秒过期 // 当前使用的 RTM Token 还有 30 秒过期
@Override @Override
public void onTokenPrivilegeWillExpire() { public void onTokenPrivilegeWillExpire() {
RedisUtil.deleteLike(RedisConst.RTM_RESIDENT_USER_TOKEN_PREFIX); RedisUtil.delete(RedisConst.RTM_RESIDENT_USER_TOKEN_PREFIX + UID);
RedisUtil.deleteLike(RedisConst.RTM_RESIDENT_USER_LOGIN_STATUS_PREFIX); RedisUtil.delete(RedisConst.RTM_RESIDENT_USER_LOGIN_STATUS_PREFIX + UID);
logout();
} }
@Override @Override
...@@ -57,13 +59,12 @@ public class RtmInstance { ...@@ -57,13 +59,12 @@ public class RtmInstance {
log.error("RtmClient初始化失败", e); log.error("RtmClient初始化失败", e);
throw new RuntimeException("Need to check rtm sdk init process"); throw new RuntimeException("Need to check rtm sdk init process");
} }
rtmClient.setLogFilter(RtmClient.LOG_FILTER_OFF); RTM_CLIENT.setLogFilter(RtmClient.LOG_FILTER_OFF);
log.info("rtm sdk init success"); log.info("rtm sdk init success");
return rtmClient;
} }
public static void logout() { public static void logout() {
rtmClient.logout(new ResultCallback<Void>() { RTM_CLIENT.logout(new ResultCallback<Void>() {
@Override @Override
public void onSuccess(Void responseInfo) { public void onSuccess(Void responseInfo) {
log.info("logout success!"); log.info("logout success!");
...@@ -80,7 +81,7 @@ public class RtmInstance { ...@@ -80,7 +81,7 @@ public class RtmInstance {
String key = RedisConst.RTM_RESIDENT_USER_LOGIN_STATUS_PREFIX + userId; String key = RedisConst.RTM_RESIDENT_USER_LOGIN_STATUS_PREFIX + userId;
String value = RedisUtil.get(key); String value = RedisUtil.get(key);
if (StringUtils.isBlank(value)) { if (StringUtils.isBlank(value)) {
rtmClient.login(token, userId, new ResultCallback<Void>() { RTM_CLIENT.login(token, userId, new ResultCallback<Void>() {
@Override @Override
public void onSuccess(Void responseInfo) { public void onSuccess(Void responseInfo) {
log.info("{} login success!", userId); log.info("{} login success!", userId);
...@@ -99,9 +100,9 @@ public class RtmInstance { ...@@ -99,9 +100,9 @@ public class RtmInstance {
} }
public static void sendPeerMessage(String dst, String message) { public static void sendPeerMessage(String dst, String message) {
RtmMessage msg = rtmClient.createMessage(); RtmMessage msg = RTM_CLIENT.createMessage();
msg.setText(message); msg.setText(message);
rtmClient.sendMessageToPeer(dst, msg, new ResultCallback<Void>() { RTM_CLIENT.sendMessageToPeer(dst, msg, new ResultCallback<Void>() {
@Override @Override
public void onSuccess(Void aVoid) { public void onSuccess(Void aVoid) {
log.info("send message to peer success, message: {}!", message); log.info("send message to peer success, message: {}!", message);
...@@ -114,4 +115,14 @@ public class RtmInstance { ...@@ -114,4 +115,14 @@ public class RtmInstance {
}); });
} }
@Value("${shengwang.appId}")
public void setAppId(String appId) {
RtmInstance.APP_ID = appId;
}
@Value("${shengwang.uid}")
public void setUID(String UID) {
RtmInstance.UID = UID;
}
} }
\ No newline at end of file
package com.makeit.startup.runner; package com.makeit.startup.runner;
import com.makeit.config.ShengwangProperties;
import com.makeit.enums.redis.RedisConst; import com.makeit.enums.redis.RedisConst;
import com.makeit.utils.redis.RedisUtil; import com.makeit.utils.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner; import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -9,10 +11,14 @@ import org.springframework.stereotype.Component; ...@@ -9,10 +11,14 @@ import org.springframework.stereotype.Component;
@Component @Component
public class RtmRunner implements ApplicationRunner { public class RtmRunner implements ApplicationRunner {
@Autowired
private ShengwangProperties shengwangProperties;
@Override @Override
public void run(ApplicationArguments args) throws Exception { public void run(ApplicationArguments args) throws Exception {
RedisUtil.deleteLike(RedisConst.RTM_RESIDENT_USER_TOKEN_PREFIX); String uid = shengwangProperties.getUid();
RedisUtil.deleteLike(RedisConst.RTM_RESIDENT_USER_LOGIN_STATUS_PREFIX); RedisUtil.delete(RedisConst.RTM_RESIDENT_USER_TOKEN_PREFIX + uid);
RedisUtil.delete(RedisConst.RTM_RESIDENT_USER_LOGIN_STATUS_PREFIX + uid);
} }
} }
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