-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
[moon]
committed
Jul 18, 2022
1 parent
44446db
commit 95fde6c
Showing
9 changed files
with
189 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/plus/jdk/websocket/global/SessionGroupManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package plus.jdk.websocket.global; | ||
|
||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import lombok.extern.slf4j.Slf4j; | ||
import plus.jdk.websocket.model.ChannelModel; | ||
import plus.jdk.websocket.model.IWsSession; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentLinkedDeque; | ||
|
||
@Slf4j | ||
public class SessionGroupManager { | ||
|
||
private final ConcurrentHashMap<Channel, ChannelModel> channelModelMap = new ConcurrentHashMap<>(); | ||
|
||
private final ConcurrentHashMap<Object, ConcurrentHashMap<String, ConcurrentLinkedDeque<IWsSession<?>>>> sessionMap = new ConcurrentHashMap<>(); | ||
|
||
protected void addSession(String path, IWsSession<?> iWsSession) { | ||
Object uid = iWsSession.getUserId(); | ||
sessionMap.putIfAbsent(uid, new ConcurrentHashMap<>()); | ||
sessionMap.get(uid).putIfAbsent(path, new ConcurrentLinkedDeque<>()); | ||
sessionMap.get(uid).get(path).add(iWsSession); | ||
channelModelMap.put(iWsSession.getChannel(), new ChannelModel(uid, path)); | ||
} | ||
|
||
public <T> ConcurrentLinkedDeque<IWsSession<?>> getSession(T userId, String path) { | ||
ConcurrentHashMap<String, ConcurrentLinkedDeque<IWsSession<?>>> userSessionMap = sessionMap.get(userId); | ||
if (userSessionMap == null || userSessionMap.get(path) == null) { | ||
return new ConcurrentLinkedDeque<>(); | ||
} | ||
return sessionMap.get(userId).get(path); | ||
} | ||
|
||
protected void releaseChannel(ChannelHandlerContext ctx) { | ||
Channel channel = ctx.channel(); | ||
ChannelModel channelModel = channelModelMap.get(channel); | ||
Object uid = channelModel.getUserId(); | ||
String path = channelModel.getPath(); | ||
sessionMap.putIfAbsent(uid, new ConcurrentHashMap<>()); | ||
sessionMap.get(uid).putIfAbsent(path, new ConcurrentLinkedDeque<>()); | ||
ConcurrentLinkedDeque<IWsSession<?>> wsSessionsDeque = sessionMap.get(uid).get(path); | ||
if (wsSessionsDeque == null) { | ||
return; | ||
} | ||
wsSessionsDeque.removeIf(iWsSession -> iWsSession.getChannel() == channel); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package plus.jdk.websocket.model; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
public class ChannelModel { | ||
|
||
/** | ||
* 当前的uid | ||
*/ | ||
private Object userId; | ||
|
||
/** | ||
* websocket路径 | ||
*/ | ||
private String path; | ||
} |