com.github.sseserver.local.LocalConnectionService Maven / Gradle / Ivy
package com.github.sseserver.local;
import com.github.sseserver.ConnectionQueryService;
import com.github.sseserver.DistributedConnectionService;
import com.github.sseserver.SendService;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Consumer;
/**
* 长连接
* 1. 如果用nginx代理, 要加下面的配置
* # 长连接配置
* proxy_buffering off;
* proxy_read_timeout 7200s;
* proxy_pass http://xx.xx.xx.xx:xxx;
* proxy_http_version 1.1; #nginx默认是http1.0, 改为1.1 支持长连接, 和后端保持长连接,复用,防止出现文件句柄打开数量过多的错误
* proxy_set_header Connection ""; # 去掉Connection的close字段
*
* @author hao 2021年12月7日19:27:41
*/
public interface LocalConnectionService extends DistributedConnectionService, ConnectionQueryService, SendService {
ScheduledExecutorService getScheduled();
/* connect */
/**
* 创建用户连接并返回 SseEmitter
*
* @param accessUser 用户令牌
* @param keepaliveTime 链接最大保持时间 ,0表示不过期。默认30秒,超过时间未完成会抛出异常:AsyncRequestTimeoutException
* @param attributeMap {@link SseEmitter#getAttributeMap()}
* @return SseEmitter
*/
SseEmitter connect(ACCESS_USER accessUser, Long keepaliveTime, Map attributeMap);
/* disconnect */
List> disconnectByUserId(Serializable userId);
List> disconnectByAccessToken(String accessToken);
SseEmitter disconnectByConnectionId(Long connectionId);
SseEmitter disconnectByConnectionId(Long connectionId, Long duration, Long sessionDuration);
List> disconnectByConnectionIds(Collection connectionIds);
/**
* 清零已统计的在线时长
*
* @param userId 用户ID
* @param 用户
* @return 清空的链接
*/
default List> clearDurationByUserId(Serializable userId) {
return setDurationByUserId(userId, 0L);
}
default List> clearDurationByAccessToken(String accessToken) {
return setDurationByAccessToken(accessToken, 0L);
}
default SseEmitter clearDurationByConnectionId(Long connectionId) {
return setDurationByConnectionId(connectionId, 0L);
}
/**
* 修改已统计的在线时长
*
* @param userId 用户ID
* @param durationSecond 在线时长(秒)
* @param 用户
* @return 清空的链接
*/
List> setDurationByUserId(Serializable userId, long durationSecond);
List> setDurationByAccessToken(String accessToken, long durationSecond);
SseEmitter setDurationByConnectionId(Long connectionId, long durationSecond);
/* getConnectionId */
Collection getConnectionIds();
/* getConnection */
Collection> getConnectionAll();
SseEmitter getConnectionById(Long connectionId);
List> getConnectionByListening(String sseListenerName);
List> getConnectionByChannel(String channel);
List> getConnectionByAccessToken(String accessToken);
List> getConnectionByUserId(Serializable userId);
List> getConnectionByTenantId(Serializable tenantId);
/* ConnectListener */
void addConnectListener(String accessToken, String channel, Consumer> consumer);
void addConnectListener(String accessToken, Consumer> consumer);
void addConnectListener(Consumer> consumer);
/* DisConnectListener */
void addDisConnectListener(Consumer> consumer);
void addDisConnectListener(String accessToken, Consumer> consumer);
/* ListeningChangeWatch */
void addListeningChangeWatch(Consumer>> watch);
}