Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
黄嘉阳
/
iot-platform-server
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
d23c5aa8
authored
Mar 27, 2024
by
罗志长
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
fix: rtm优化
parent
adf4f15f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
19 deletions
server-common/src/main/java/com/makeit/shengwang/agora/rtm/RtmInstance.java
server-common/src/main/java/com/makeit/startup/runner/RtmRunner.java
server-common/src/main/java/com/makeit/shengwang/agora/rtm/RtmInstance.java
View file @
d23c5aa8
...
...
@@ -5,27 +5,28 @@ import com.makeit.utils.old.StringUtils;
import
com.makeit.utils.redis.RedisUtil
;
import
io.agora.rtm.*
;
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.concurrent.TimeUnit
;
@Slf4j
@Component
public
class
RtmInstance
{
public
static
final
String
APP_ID
=
"883078934ecd4193aa7a62a3cdacd810"
;
private
static
final
RtmClient
rtmClient
;
static
{
rtmClient
=
init
();
}
private
static
String
APP_ID
;
private
static
String
UID
;
private
static
RtmClient
RTM_CLIENT
;
private
RtmInstance
()
{
}
private
static
RtmClient
init
()
{
RtmClient
rtmClient
=
null
;
@PostConstruct
private
void
init
()
{
try
{
rtmClient
=
RtmClient
.
createInstance
(
APP_ID
,
new
RtmClientListener
()
{
RTM_CLIENT
=
RtmClient
.
createInstance
(
APP_ID
,
new
RtmClientListener
()
{
@Override
public
void
onConnectionStateChanged
(
int
state
,
int
reason
)
{
log
.
info
(
"on connection state changed to "
+
state
+
" reason: "
+
reason
);
...
...
@@ -45,8 +46,9 @@ public class RtmInstance {
// 当前使用的 RTM Token 还有 30 秒过期
@Override
public
void
onTokenPrivilegeWillExpire
()
{
RedisUtil
.
deleteLike
(
RedisConst
.
RTM_RESIDENT_USER_TOKEN_PREFIX
);
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
);
logout
();
}
@Override
...
...
@@ -57,13 +59,12 @@ public class RtmInstance {
log
.
error
(
"RtmClient初始化失败"
,
e
);
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"
);
return
rtmClient
;
}
public
static
void
logout
()
{
rtmClient
.
logout
(
new
ResultCallback
<
Void
>()
{
RTM_CLIENT
.
logout
(
new
ResultCallback
<
Void
>()
{
@Override
public
void
onSuccess
(
Void
responseInfo
)
{
log
.
info
(
"logout success!"
);
...
...
@@ -80,7 +81,7 @@ public class RtmInstance {
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
>()
{
RTM_CLIENT
.
login
(
token
,
userId
,
new
ResultCallback
<
Void
>()
{
@Override
public
void
onSuccess
(
Void
responseInfo
)
{
log
.
info
(
"{} login success!"
,
userId
);
...
...
@@ -99,9 +100,9 @@ public class RtmInstance {
}
public
static
void
sendPeerMessage
(
String
dst
,
String
message
)
{
RtmMessage
msg
=
rtmClient
.
createMessage
();
RtmMessage
msg
=
RTM_CLIENT
.
createMessage
();
msg
.
setText
(
message
);
rtmClient
.
sendMessageToPeer
(
dst
,
msg
,
new
ResultCallback
<
Void
>()
{
RTM_CLIENT
.
sendMessageToPeer
(
dst
,
msg
,
new
ResultCallback
<
Void
>()
{
@Override
public
void
onSuccess
(
Void
aVoid
)
{
log
.
info
(
"send message to peer success, message: {}!"
,
message
);
...
...
@@ -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
server-common/src/main/java/com/makeit/startup/runner/RtmRunner.java
View file @
d23c5aa8
package
com
.
makeit
.
startup
.
runner
;
import
com.makeit.config.ShengwangProperties
;
import
com.makeit.enums.redis.RedisConst
;
import
com.makeit.utils.redis.RedisUtil
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.ApplicationArguments
;
import
org.springframework.boot.ApplicationRunner
;
import
org.springframework.stereotype.Component
;
...
...
@@ -9,10 +11,14 @@ import org.springframework.stereotype.Component;
@Component
public
class
RtmRunner
implements
ApplicationRunner
{
@Autowired
private
ShengwangProperties
shengwangProperties
;
@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
);
String
uid
=
shengwangProperties
.
getUid
();
RedisUtil
.
delete
(
RedisConst
.
RTM_RESIDENT_USER_TOKEN_PREFIX
+
uid
);
RedisUtil
.
delete
(
RedisConst
.
RTM_RESIDENT_USER_LOGIN_STATUS_PREFIX
+
uid
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment