Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
beyond-clouds
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
4
Issues
4
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
段启岩
beyond-clouds
Commits
501b67f3
Commit
501b67f3
authored
Feb 23, 2020
by
段启岩
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
消息更新
parent
5ce23bc4
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
7 deletions
+19
-7
src/main/java/cn/meteor/beyondclouds/modules/im/server/SocketIOServiceImpl.java
+19
-7
No files found.
src/main/java/cn/meteor/beyondclouds/modules/im/server/SocketIOServiceImpl.java
View file @
501b67f3
...
@@ -9,10 +9,13 @@ import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
...
@@ -9,10 +9,13 @@ import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.CollectionUtils
;
import
javax.annotation.PostConstruct
;
import
javax.annotation.PostConstruct
;
import
javax.annotation.PreDestroy
;
import
javax.annotation.PreDestroy
;
import
java.util.HashSet
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.ConcurrentHashMap
;
/**
/**
...
@@ -25,7 +28,7 @@ public class SocketIOServiceImpl implements SocketIOService {
...
@@ -25,7 +28,7 @@ public class SocketIOServiceImpl implements SocketIOService {
/**
/**
* 用来存已连接的客户端
* 用来存已连接的客户端
*/
*/
private
static
final
Map
<
String
,
S
ocketIOClient
>
CLIENT_MAP
=
new
ConcurrentHashMap
<>();
private
static
final
Map
<
String
,
S
et
<
SocketIOClient
>
>
CLIENT_MAP
=
new
ConcurrentHashMap
<>();
private
SocketIOServer
socketIOServer
;
private
SocketIOServer
socketIOServer
;
...
@@ -81,7 +84,7 @@ public class SocketIOServiceImpl implements SocketIOService {
...
@@ -81,7 +84,7 @@ public class SocketIOServiceImpl implements SocketIOService {
}
}
// 将session存入内存
// 将session存入内存
CLIENT_MAP
.
pu
t
(
userId
,
client
);
putClien
t
(
userId
,
client
);
log
.
info
(
"user {} connected to the im server"
,
userId
);
log
.
info
(
"user {} connected to the im server"
,
userId
);
// client.sendEvent(PUSH_EVENT, "hello, 欢迎连接服务器!");
// client.sendEvent(PUSH_EVENT, "hello, 欢迎连接服务器!");
});
});
...
@@ -93,7 +96,10 @@ public class SocketIOServiceImpl implements SocketIOService {
...
@@ -93,7 +96,10 @@ public class SocketIOServiceImpl implements SocketIOService {
if
(
null
!=
token
)
{
if
(
null
!=
token
)
{
String
userId
=
tokenManager
.
getUserId
(
token
);
String
userId
=
tokenManager
.
getUserId
(
token
);
if
(
null
!=
userId
)
{
if
(
null
!=
userId
)
{
// CLIENT_MAP.remove(userId);
Set
<
SocketIOClient
>
clients
=
CLIENT_MAP
.
get
(
userId
);
if
(!
CollectionUtils
.
isEmpty
(
clients
))
{
clients
.
remove
(
client
);
}
}
}
}
}
});
});
...
@@ -114,11 +120,17 @@ public class SocketIOServiceImpl implements SocketIOService {
...
@@ -114,11 +120,17 @@ public class SocketIOServiceImpl implements SocketIOService {
@Override
@Override
public
void
pushMessage
(
String
userId
,
Object
message
)
{
public
void
pushMessage
(
String
userId
,
Object
message
)
{
SocketIOClient
client
=
CLIENT_MAP
.
get
(
userId
);
Set
<
SocketIOClient
>
clients
=
CLIENT_MAP
.
get
(
userId
);
log
.
info
(
"send message to {}"
,
client
);
log
.
info
(
"send message to {}"
,
userId
);
if
(
null
!=
client
)
{
if
(!
CollectionUtils
.
isEmpty
(
clients
))
{
client
.
sendEvent
(
PUSH_EVENT
,
message
);
clients
.
forEach
(
socketIOClient
->
{
socketIOClient
.
sendEvent
(
PUSH_EVENT
,
message
);
});;
}
}
}
private
void
putClient
(
String
userId
,
SocketIOClient
socketIOClient
)
{
CLIENT_MAP
.
getOrDefault
(
userId
,
new
HashSet
<>()).
add
(
socketIOClient
);
}
}
}
}
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