Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
wecloud_im_server
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hewei
wecloud_im_server
Commits
22832b70
Commit
22832b70
authored
Apr 27, 2022
by
罗长华
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
完善回调功能
parent
544bcd9a
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
60 additions
and
30 deletions
+60
-30
core/src/main/java/com/wecloud/im/event/ClientOnlineStatusChangeEvent.java
+2
-2
core/src/main/java/com/wecloud/im/event/listener/ClientOnlineStatusChangeListener.java
+3
-1
core/src/main/java/com/wecloud/im/netty/core/WsReadHandler.java
+14
-1
core/src/main/java/com/wecloud/im/netty/handler/NettyApiRequest.java
+10
-9
core/src/main/java/com/wecloud/im/service/ImCallbackService.java
+1
-1
core/src/main/java/com/wecloud/im/service/impl/ImCallbackServiceImpl.java
+22
-12
core/src/main/java/com/wecloud/im/ws/manager/ChannelManager.java
+8
-4
No files found.
core/src/main/java/com/wecloud/im/event/ClientOnlineStatusChangeEvent.java
View file @
22832b70
...
...
@@ -16,7 +16,7 @@ public class ClientOnlineStatusChangeEvent extends ApplicationEvent {
private
Long
applicationId
;
private
Stri
ng
clientId
;
private
Lo
ng
clientId
;
private
Integer
status
;
...
...
@@ -35,7 +35,7 @@ public class ClientOnlineStatusChangeEvent extends ApplicationEvent {
super
(
source
);
}
public
ClientOnlineStatusChangeEvent
(
Long
applicationId
,
Stri
ng
clientId
,
public
ClientOnlineStatusChangeEvent
(
Long
applicationId
,
Lo
ng
clientId
,
Integer
status
,
Integer
deviceType
,
Long
time
,
String
clientIp
)
{
super
(
clientId
);
this
.
applicationId
=
applicationId
;
...
...
core/src/main/java/com/wecloud/im/event/listener/ClientOnlineStatusChangeListener.java
View file @
22832b70
...
...
@@ -2,6 +2,7 @@ package com.wecloud.im.event.listener;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.ApplicationListener
;
import
org.springframework.scheduling.annotation.Async
;
import
org.springframework.stereotype.Component
;
import
com.wecloud.im.event.ClientOnlineStatusChangeEvent
;
...
...
@@ -19,11 +20,12 @@ public class ClientOnlineStatusChangeListener implements ApplicationListener<Cli
@Autowired
private
ImCallbackService
callbackService
;
@Async
@Override
public
void
onApplicationEvent
(
ClientOnlineStatusChangeEvent
event
)
{
Long
applicationId
=
event
.
getApplicationId
();
Stri
ng
clientId
=
event
.
getClientId
();
Lo
ng
clientId
=
event
.
getClientId
();
Integer
status
=
event
.
getStatus
();
...
...
core/src/main/java/com/wecloud/im/netty/core/WsReadHandler.java
View file @
22832b70
...
...
@@ -9,9 +9,12 @@ import lombok.extern.slf4j.Slf4j;
import
javax.annotation.Resource
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.ApplicationEventPublisher
;
import
org.springframework.stereotype.Component
;
import
com.wecloud.dispatch.general.GeneralMessageHandler
;
import
com.wecloud.im.event.ClientOnlineStatusChangeEvent
;
import
com.wecloud.im.executor.BusinessThreadPool
;
import
com.wecloud.im.ws.manager.ChannelManager
;
...
...
@@ -35,6 +38,9 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram
@Resource
private
GeneralMessageHandler
generalMessageHandler
;
@Autowired
private
ApplicationEventPublisher
eventPublisher
;
@Override
protected
void
channelRead0
(
ChannelHandlerContext
ctx
,
TextWebSocketFrame
msg
)
{
// 读空闲的计数清零
...
...
@@ -135,14 +141,21 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram
@Override
public
void
handlerRemoved
(
ChannelHandlerContext
ctx
)
{
Long
appId
=
ctx
.
channel
().
attr
(
ChannelManager
.
APPLICATION_ID
).
get
();
Long
clientId
=
ctx
.
channel
().
attr
(
ChannelManager
.
CLIENT_ID
).
get
();
Integer
platform
=
ctx
.
channel
().
attr
(
ChannelManager
.
PLATFORM
).
get
();
log
.
info
(
"CLIENT_ID:{}, handlerRemoved. channelId is {}"
,
clientId
,
ctx
.
channel
().
id
().
asLongText
());
// 关掉连接
channelManager
.
offline
(
clientId
,
platform
,
ctx
);
// 下线通知
long
time
=
System
.
currentTimeMillis
();
String
clientIp
=
ctx
.
channel
().
remoteAddress
().
toString
();
ClientOnlineStatusChangeEvent
clientOnlineStatusChangeEvent
=
new
ClientOnlineStatusChangeEvent
(
appId
,
clientId
,
0
,
platform
,
time
,
clientIp
);
eventPublisher
.
publishEvent
(
clientOnlineStatusChangeEvent
);
}
}
core/src/main/java/com/wecloud/im/netty/handler/NettyApiRequest.java
View file @
22832b70
...
...
@@ -158,20 +158,21 @@ public class NettyApiRequest {
// 设置属性值 userid - channel
ctx
.
channel
().
attr
(
ChannelManager
.
CLIENT_ID
).
set
(
client
.
getId
());
ctx
.
channel
().
attr
(
ChannelManager
.
PLATFORM
).
set
(
jwtToken
.
getPlatform
());
ctx
.
channel
().
attr
(
ChannelManager
.
APPLICATION_ID
).
set
(
app
.
getId
());
// 读空闲的计数=0
ctx
.
channel
().
attr
(
ChannelManager
.
READ_IDLE_TIMES
).
set
(
0
);
// 读空闲的计数=0
// 保存用户上下文对象
appUserChannelsService
.
online
(
client
.
getId
(),
jwtToken
.
getPlatform
(),
(
NioSocketChannel
)
ctx
.
channel
());
// 发布客户端在线状态变化事件
//
Long appId = app.getId();
//
String clientId = client.getClientId();
//
Integer platform = jwtToken.getPlatform();
//
long time = System.currentTimeMillis();
//
String clientIp = ctx.channel().remoteAddress().toString();
//
ClientOnlineStatusChangeEvent clientOnlineStatusChangeEvent = new ClientOnlineStatusChangeEvent(appId,
// clientId
, 1, platform, time, clientIp);
//
eventPublisher.publishEvent(clientOnlineStatusChangeEvent);
// 发布客户端在线状态变化
-上线
事件
Long
appId
=
app
.
getId
();
String
clientId
=
client
.
getClientId
();
Integer
platform
=
jwtToken
.
getPlatform
();
long
time
=
System
.
currentTimeMillis
();
String
clientIp
=
ctx
.
channel
().
remoteAddress
().
toString
();
ClientOnlineStatusChangeEvent
clientOnlineStatusChangeEvent
=
new
ClientOnlineStatusChangeEvent
(
appId
,
client
.
getId
()
,
1
,
platform
,
time
,
clientIp
);
eventPublisher
.
publishEvent
(
clientOnlineStatusChangeEvent
);
//移除当前api处理handler, 不再参与长连接处理
ctx
.
pipeline
().
remove
(
"SingleHttpRequestHandler"
);
...
...
core/src/main/java/com/wecloud/im/service/ImCallbackService.java
View file @
22832b70
...
...
@@ -24,6 +24,6 @@ public interface ImCallbackService {
* @param
* @Return
*/
Boolean
clientOnlineStatusChange
(
Long
applicationId
,
Stri
ng
clientId
,
Integer
status
,
Boolean
clientOnlineStatusChange
(
Long
applicationId
,
Lo
ng
clientId
,
Integer
status
,
Integer
deviceType
,
Long
time
,
String
clientIp
);
}
core/src/main/java/com/wecloud/im/service/impl/ImCallbackServiceImpl.java
View file @
22832b70
package
com
.
wecloud
.
im
.
service
.
impl
;
import
io.geekidea.springbootplus.framework.common.exception.BusinessException
;
import
io.geekidea.springbootplus.framework.shiro.signature.SignUtils
;
import
java.util.Date
;
...
...
@@ -16,9 +15,11 @@ import cn.hutool.core.date.DateUtil;
import
cn.hutool.core.lang.id.NanoId
;
import
com.wecloud.im.entity.ImApplication
;
import
com.wecloud.im.entity.ImClient
;
import
com.wecloud.im.param.ClientOnlineStatusChangeDto
;
import
com.wecloud.im.service.ImApplicationService
;
import
com.wecloud.im.service.ImCallbackService
;
import
com.wecloud.im.service.ImClientService
;
/**
* 回调服务实现类
...
...
@@ -35,6 +36,9 @@ public class ImCallbackServiceImpl implements ImCallbackService {
@Autowired
private
ImApplicationService
applicationService
;
@Autowired
private
ImClientService
imClientService
;
/**
* 全量消息路由
* @Author luozh
...
...
@@ -53,41 +57,47 @@ public class ImCallbackServiceImpl implements ImCallbackService {
* @Date 2022年04月22日 09:35:47
* @param applicationId 应用id
* @param clientId 客户端id
* @param status 状态:
0:online 上线、1
:offline 离线、2:logout 登出
* @param status 状态:
1:online 上线、0
:offline 离线、2:logout 登出
* @param deviceType 设备类型
* @param time 发生时间
* @param clientIp 用户当前的 IP 地址及端口
* @Return
*/
@Override
public
Boolean
clientOnlineStatusChange
(
Long
applicationId
,
Stri
ng
clientId
,
Integer
status
,
public
Boolean
clientOnlineStatusChange
(
Long
applicationId
,
Lo
ng
clientId
,
Integer
status
,
Integer
deviceType
,
Long
time
,
String
clientIp
)
{
ImApplication
application
=
applicationService
.
getById
(
applicationId
);
if
(
application
==
null
)
{
throw
new
BusinessException
(
"application not exist"
)
;
return
false
;
}
ImClient
client
=
imClientService
.
getCacheImClient
(
clientId
);
String
subscribeUrl
=
application
.
getOnlineStatusSubscribeUrl
();
if
(
StringUtils
.
isNotBlank
(
subscribeUrl
))
{
String
appKey
=
application
.
getAppKey
();
String
appSecret
=
application
.
getAppSecret
();
String
callbackUrl
=
buildCallbackUrl
(
subscribeUrl
,
appKey
,
appSecret
);
ClientOnlineStatusChangeDto
body
=
ClientOnlineStatusChangeDto
.
builder
()
.
userId
(
client
Id
)
.
userId
(
client
.
getClientId
()
)
.
status
(
status
)
.
os
(
""
)
.
time
(
time
)
.
clientIp
(
clientIp
)
.
build
();
ResponseEntity
<
Object
>
response
=
restTemplate
.
postForEntity
(
callbackUrl
,
body
,
Object
.
class
);
// 同步在线状态时需要接收服务提供应答,只要有 HTTP 应答码 200 即认为状态已经同步
if
(
response
.
getStatusCode
().
equals
(
HttpStatus
.
OK
))
{
// do nothing
}
else
{
// 如果应答超时 5 秒,会再尝试推送 2 次,如果仍然失败,将不再同步此条状态。如短时间内有大面积超时,将暂停推送,1 分钟后会继续推送。
try
{
ResponseEntity
<
Object
>
response
=
restTemplate
.
postForEntity
(
callbackUrl
,
body
,
Object
.
class
);
// 同步在线状态时需要接收服务提供应答,只要有 HTTP 应答码 200 即认为状态已经同步
if
(
response
.
getStatusCode
().
equals
(
HttpStatus
.
OK
))
{
// do nothing
}
else
{
// 如果应答超时 5 秒,会再尝试推送 2 次,如果仍然失败,将不再同步此条状态。如短时间内有大面积超时,将暂停推送,1 分钟后会继续推送。
}
}
catch
(
Exception
e
)
{
// do nothing is ok
}
}
return
true
;
...
...
core/src/main/java/com/wecloud/im/ws/manager/ChannelManager.java
View file @
22832b70
package
com
.
wecloud
.
im
.
ws
.
manager
;
import
com.wecloud.im.ws.cache.UserStateListener
;
import
com.wecloud.im.ws.model.ClientInfo
;
import
com.wecloud.im.ws.utils.RedisUtils
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.channel.socket.nio.NioSocketChannel
;
import
io.netty.util.AttributeKey
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.stereotype.Component
;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
import
org.springframework.stereotype.Component
;
import
com.wecloud.im.ws.cache.UserStateListener
;
import
com.wecloud.im.ws.model.ClientInfo
;
import
com.wecloud.im.ws.utils.RedisUtils
;
/**
* channel内容管理,在线、离线等信息在channel里
* @author lixiaozhong
...
...
@@ -32,6 +34,8 @@ public class ChannelManager {
*/
public
static
final
AttributeKey
<
Long
>
CLIENT_ID
=
AttributeKey
.
valueOf
(
"ci"
);
public
static
final
AttributeKey
<
Long
>
APPLICATION_ID
=
AttributeKey
.
valueOf
(
"ai"
);
public
static
final
AttributeKey
<
Integer
>
READ_IDLE_TIMES
=
AttributeKey
.
valueOf
(
"readIdleTimes"
);
/**
...
...
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