WebSocket增加getUserSet方法

This commit is contained in:
Redkale
2020-01-07 13:07:04 +08:00
parent df98c1a58e
commit ad1d9f33d4
2 changed files with 22 additions and 1 deletions

View File

@@ -452,6 +452,11 @@ public class WebSocketEngine {
return (int) websockets2.values().stream().mapToInt(sublist -> sublist.size()).count();
}
@Comment("获取当前用户总数")
public Set<Serializable> getLocalUserSet() {
return single ? new LinkedHashSet<>(websockets.keySet()) : new LinkedHashSet<>(websockets2.keySet());
}
@Comment("获取当前用户总数")
public int getLocalUserSize() {
return single ? websockets.size() : websockets2.size();

View File

@@ -11,7 +11,7 @@ import java.net.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.logging.*;
import java.util.stream.Stream;
import java.util.stream.*;
import javax.annotation.*;
import org.redkale.boot.*;
import org.redkale.convert.*;
@@ -208,6 +208,22 @@ public abstract class WebSocketNode {
return rs;
}
/**
* 获取在线用户总数
*
*
* @return boolean
*/
public CompletableFuture<Set<String>> getUserSet() {
if (this.localEngine != null && this.sncpNodeAddresses == null) {
return CompletableFuture.completedFuture(new LinkedHashSet<>(this.localEngine.getLocalUserSet().stream().map(x -> String.valueOf(x)).collect(Collectors.toList())));
}
tryAcquireSemaphore();
CompletableFuture<Set<String>> rs = this.sncpNodeAddresses.queryKeysStartsWithAsync(SOURCE_SNCP_USERID_PREFIX).thenApply(v -> new LinkedHashSet<>(v.stream().map(x -> x.substring(SOURCE_SNCP_USERID_PREFIX.length())).collect(Collectors.toList())));
if (semaphore != null) rs.whenComplete((r, e) -> releaseSemaphore());
return rs;
}
/**
* 判断指定用户是否WebSocket在线
*