🐛 修复发送消息报错.
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
package com.orion.visor.framework.websocket.core.session;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.web.socket.CloseStatus;
|
||||
import org.springframework.web.socket.WebSocketExtension;
|
||||
import org.springframework.web.socket.WebSocketMessage;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.security.Principal;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* web socket 同步会话
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2024/5/20 10:12
|
||||
*/
|
||||
public class WebSocketSyncSession implements WebSocketSession {
|
||||
|
||||
private final WebSocketSession delegate;
|
||||
|
||||
public WebSocketSyncSession(WebSocketSession delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return this.delegate.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getUri() {
|
||||
return this.delegate.getUri();
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHandshakeHeaders() {
|
||||
return this.delegate.getHandshakeHeaders();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getAttributes() {
|
||||
return this.delegate.getAttributes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Principal getPrincipal() {
|
||||
return this.delegate.getPrincipal();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InetSocketAddress getLocalAddress() {
|
||||
return this.delegate.getLocalAddress();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InetSocketAddress getRemoteAddress() {
|
||||
return this.delegate.getRemoteAddress();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAcceptedProtocol() {
|
||||
return this.delegate.getAcceptedProtocol();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTextMessageSizeLimit(int messageSizeLimit) {
|
||||
this.delegate.setTextMessageSizeLimit(messageSizeLimit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTextMessageSizeLimit() {
|
||||
return this.delegate.getTextMessageSizeLimit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBinaryMessageSizeLimit(int messageSizeLimit) {
|
||||
this.delegate.setBinaryMessageSizeLimit(messageSizeLimit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBinaryMessageSizeLimit() {
|
||||
return this.delegate.getBinaryMessageSizeLimit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<WebSocketExtension> getExtensions() {
|
||||
return this.delegate.getExtensions();
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void sendMessage(WebSocketMessage<?> message) throws IOException {
|
||||
this.delegate.sendMessage(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOpen() {
|
||||
return this.delegate.isOpen();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
this.delegate.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close(CloseStatus status) throws IOException {
|
||||
this.delegate.close(status);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,7 +3,9 @@ package com.orion.visor.framework.websocket.core.utils;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.orion.lang.utils.Exceptions;
|
||||
import com.orion.lang.utils.Threads;
|
||||
import com.orion.visor.framework.common.constant.Const;
|
||||
import com.orion.visor.framework.websocket.core.constant.WsCloseCode;
|
||||
import com.orion.visor.framework.websocket.core.session.WebSocketSyncSession;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.socket.CloseStatus;
|
||||
import org.springframework.web.socket.TextMessage;
|
||||
@@ -24,6 +26,16 @@ public class WebSockets {
|
||||
private WebSockets() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建同步会话
|
||||
*
|
||||
* @param session session
|
||||
* @return session
|
||||
*/
|
||||
public static WebSocketSession createSyncSession(WebSocketSession session) {
|
||||
return new WebSocketSyncSession(session);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取属性
|
||||
*
|
||||
@@ -58,13 +70,13 @@ public class WebSockets {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// 发重消息
|
||||
// 发送消息
|
||||
session.sendMessage(new TextMessage(message));
|
||||
} catch (IllegalStateException e) {
|
||||
// 并发异常
|
||||
log.error("发送消息失败, 准备进行重试 {}", Exceptions.getDigest(e));
|
||||
// 并发重试
|
||||
retrySendText(session, message, 50);
|
||||
retrySendText(session, message, Const.MS_100);
|
||||
} catch (IOException e) {
|
||||
throw Exceptions.ioRuntime(e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user