[*] 日常优化
This commit is contained in:
@@ -40,6 +40,10 @@ public abstract class ClientAdapter<T extends AutoCloseable> implements Client {
|
||||
* 终端实例
|
||||
*/
|
||||
protected final T instance;
|
||||
/**
|
||||
* 是否关闭
|
||||
*/
|
||||
protected boolean close;
|
||||
/**
|
||||
* 是否授权
|
||||
*/
|
||||
@@ -62,6 +66,7 @@ public abstract class ClientAdapter<T extends AutoCloseable> implements Client {
|
||||
this.time = System.currentTimeMillis();
|
||||
this.timeout = timeout;
|
||||
this.instance = instance;
|
||||
this.close = false;
|
||||
this.authorized = false;
|
||||
this.status = new ClientStatus();
|
||||
this.requestMessage = new ConcurrentHashMap<>();
|
||||
@@ -152,6 +157,10 @@ public abstract class ClientAdapter<T extends AutoCloseable> implements Client {
|
||||
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
if(this.close) {
|
||||
return;
|
||||
}
|
||||
this.close = true;
|
||||
log.info("关闭终端实例:{} - {}", this.ip, this.clientId);
|
||||
this.instance.close();
|
||||
}
|
||||
|
||||
@@ -24,43 +24,47 @@ import lombok.extern.slf4j.Slf4j;
|
||||
@Slf4j
|
||||
@Protocol
|
||||
@Description(
|
||||
memo = "同时释放所有资源,所以如果终端意外掉线重连,需要终端实现音视频重连逻辑。",
|
||||
memo = """
|
||||
信令连接断开以后执行,同时释放所有资源。
|
||||
如果终端意外掉线,需要自己实现重连逻辑。
|
||||
""",
|
||||
flow = {
|
||||
"终端->信令服务->终端",
|
||||
"终端->信令服务-[终端下线])终端"
|
||||
"终端-[关闭终端]>信令服务-[终端下线])终端",
|
||||
"终端-[连接断开]>信令服务-[终端下线])终端"
|
||||
}
|
||||
)
|
||||
public class ClientCloseProtocol extends ProtocolClientAdapter implements ApplicationListener<ClientCloseEvent> {
|
||||
|
||||
public static final String SIGNAL = "client::close";
|
||||
|
||||
public ClientCloseProtocol() {
|
||||
super("关闭终端信令", SIGNAL);
|
||||
}
|
||||
|
||||
@Async
|
||||
@Override
|
||||
public void onApplicationEvent(ClientCloseEvent event) {
|
||||
this.close(event.getClient());
|
||||
}
|
||||
public static final String SIGNAL = "client::close";
|
||||
|
||||
public ClientCloseProtocol() {
|
||||
super("关闭终端信令", SIGNAL);
|
||||
}
|
||||
|
||||
@Async
|
||||
@Override
|
||||
public void onApplicationEvent(ClientCloseEvent event) {
|
||||
this.close(event.getClient());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String clientId, ClientType clientType, Client client, Message message, Map<String, Object> body) {
|
||||
client.push(message.cloneWithoutBody());
|
||||
try {
|
||||
// 关闭连接后会发布事件
|
||||
client.close();
|
||||
} catch (Exception e) {
|
||||
log.error("关闭终端异常:{}", clientId, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭终端
|
||||
*
|
||||
* @param client 终端
|
||||
*/
|
||||
private void close(Client client) {
|
||||
@Override
|
||||
public void execute(String clientId, ClientType clientType, Client client, Message message, Map<String, Object> body) {
|
||||
client.push(message.cloneWithoutBody());
|
||||
try {
|
||||
// 关闭连接后会发布事件
|
||||
client.close();
|
||||
} catch (Exception e) {
|
||||
log.error("关闭终端异常:{}", clientId, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭终端
|
||||
*
|
||||
* @param client 终端
|
||||
*/
|
||||
private void close(Client client) {
|
||||
if(client == null || client.unauthorized()) {
|
||||
// 没有授权终端
|
||||
return;
|
||||
@@ -73,6 +77,6 @@ public class ClientCloseProtocol extends ProtocolClientAdapter implements Applic
|
||||
this.sessionManager.close(client);
|
||||
// 终端下线事件
|
||||
this.publishEvent(new ClientOfflineEvent(client));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,60 +27,61 @@ import com.acgist.taoyao.signal.protocol.ProtocolClientAdapter;
|
||||
*/
|
||||
@Protocol
|
||||
@Description(
|
||||
memo = "终端应该在收到配置之后进行媒体操作",
|
||||
body = """
|
||||
{
|
||||
"media": "媒体配置(可选)",
|
||||
"webrtc": "WebRTC配置(可选)",
|
||||
"media" : "媒体配置(可选)",
|
||||
"webrtc" : "WebRTC配置(可选)",
|
||||
"datetime": "日期时间(yyyyMMddHHmmss)"
|
||||
}
|
||||
""",
|
||||
flow = "终端=[终端注册]>信令服务->终端"
|
||||
flow = "终端=[终端注册]>信令服务-[终端配置]>终端"
|
||||
)
|
||||
public class ClientConfigProtocol extends ProtocolClientAdapter implements ApplicationListener<ClientConfigEvent> {
|
||||
|
||||
public static final String SIGNAL = "client::config";
|
||||
|
||||
private final MediaProperties mediaProperties;
|
||||
private final WebrtcProperties webrtcProperties;
|
||||
|
||||
public ClientConfigProtocol(MediaProperties mediaProperties, WebrtcProperties webrtcProperties) {
|
||||
super("终端配置信令", SIGNAL);
|
||||
this.mediaProperties = mediaProperties;
|
||||
this.webrtcProperties = webrtcProperties;
|
||||
}
|
||||
|
||||
@Async
|
||||
@Override
|
||||
public void onApplicationEvent(ClientConfigEvent event) {
|
||||
final Client client = event.getClient();
|
||||
final ClientType clientType = client.getClientType();
|
||||
client.push(this.build(clientType));
|
||||
public static final String SIGNAL = "client::config";
|
||||
|
||||
private final MediaProperties mediaProperties;
|
||||
private final WebrtcProperties webrtcProperties;
|
||||
|
||||
public ClientConfigProtocol(MediaProperties mediaProperties, WebrtcProperties webrtcProperties) {
|
||||
super("终端配置信令", SIGNAL);
|
||||
this.mediaProperties = mediaProperties;
|
||||
this.webrtcProperties = webrtcProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String clientId, ClientType clientType, Client client, Message message, Map<String, Object> body) {
|
||||
client.push(this.build(clientType));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param clientType 终端类型
|
||||
*
|
||||
* @return 消息
|
||||
*/
|
||||
public Message build(ClientType clientType) {
|
||||
final Message message = super.build();
|
||||
final Map<String, Object> config = new HashMap<>();
|
||||
// 日期时间
|
||||
config.put(Constant.DATETIME, DateUtils.format(LocalDateTime.now(), DateTimeStyle.YYYYMMDDHH24MMSS));
|
||||
// Web、摄像头:媒体配置
|
||||
if(clientType.mediaClient()) {
|
||||
config.put(Constant.MEDIA, this.mediaProperties);
|
||||
config.put(Constant.WEBRTC, this.webrtcProperties);
|
||||
} else {
|
||||
this.logNoAdapter(clientType);
|
||||
}
|
||||
message.setBody(config);
|
||||
return message;
|
||||
}
|
||||
|
||||
|
||||
@Async
|
||||
@Override
|
||||
public void onApplicationEvent(ClientConfigEvent event) {
|
||||
final Client client = event.getClient();
|
||||
final ClientType clientType = client.getClientType();
|
||||
client.push(this.build(clientType));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String clientId, ClientType clientType, Client client, Message message, Map<String, Object> body) {
|
||||
client.push(this.build(clientType));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param clientType 终端类型
|
||||
*
|
||||
* @return 消息
|
||||
*/
|
||||
public Message build(ClientType clientType) {
|
||||
final Message message = super.build();
|
||||
final Map<String, Object> config = new HashMap<>();
|
||||
// 日期时间
|
||||
config.put(Constant.DATETIME, DateUtils.format(LocalDateTime.now(), DateTimeStyle.YYYYMMDDHH24MMSS));
|
||||
// 媒体终端:媒体配置
|
||||
if(clientType.mediaClient()) {
|
||||
config.put(Constant.MEDIA, this.mediaProperties);
|
||||
config.put(Constant.WEBRTC, this.webrtcProperties);
|
||||
} else {
|
||||
this.logNoAdapter(clientType);
|
||||
}
|
||||
message.setBody(config);
|
||||
return message;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
@Slf4j
|
||||
@Protocol
|
||||
@Description(
|
||||
memo = "收到注册响应之后应该设置终端的终端索引",
|
||||
body = """
|
||||
{
|
||||
"username": "信令用户",
|
||||
|
||||
Reference in New Issue
Block a user