连接本地localhost websocket服务成功
This commit is contained in:
parent
39e99e8d72
commit
bc69a9964e
@ -298,7 +298,7 @@ export class ChatViewProvider implements vscode.WebviewViewProvider {
|
||||
indexHtml = indexHtml.replace(
|
||||
'<head>',
|
||||
`<head>
|
||||
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src ${webview.cspSource} 'unsafe-inline'; img-src ${webview.cspSource} https: data:; script-src 'nonce-${nonce}';">`
|
||||
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; connect-src ws://localhost:8080; style-src ${webview.cspSource} 'unsafe-inline'; img-src ${webview.cspSource} https: data:; script-src 'nonce-${nonce}';">`
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -72,56 +72,15 @@ export class WebSocketService {
|
||||
// 检查是否有API密钥,如果有则添加为查询参数
|
||||
if (this.apiKey) {
|
||||
const separator = wsUrl.includes('?') ? '&' : '?';
|
||||
wsUrl += `${separator}apiKey=${encodeURIComponent(this.apiKey)}`;
|
||||
wsUrl += `${separator}api_key=${this.apiKey}`;
|
||||
}
|
||||
|
||||
// 检查环境和协议
|
||||
// const isVsCodeWebview = typeof window !== 'undefined' && 'acquireVsCodeApi' in window;
|
||||
const isLocalhost = wsUrl.includes('localhost') || wsUrl.includes('127.0.0.1');
|
||||
|
||||
// VS Code WebView环境下可能需要wss
|
||||
// if (isVsCodeWebview && wsUrl.startsWith('ws://')) {
|
||||
// this.logger('[WebSocketService] 在VS Code WebView中,尝试使用安全WebSocket连接');
|
||||
// wsUrl = wsUrl.replace('ws://', 'wss://');
|
||||
// }
|
||||
|
||||
this.logger(`[WebSocketService] 最终WebSocket URL: ${wsUrl}`);
|
||||
this.socket = new WebSocket(wsUrl);
|
||||
this.setupEventHandlers();
|
||||
|
||||
// 处理连接失败的情况
|
||||
if (isLocalhost) {
|
||||
// 如果使用localhost失败,尝试使用127.0.0.1
|
||||
const fallbackTimeout = setTimeout(() => {
|
||||
if (this.socket?.readyState !== WebSocket.OPEN) {
|
||||
this.logger('[WebSocketService] localhost连接超时,尝试使用127.0.0.1');
|
||||
|
||||
let fallbackUrl;
|
||||
if (wsUrl.includes('localhost')) {
|
||||
fallbackUrl = wsUrl.replace('localhost', '127.0.0.1');
|
||||
} else if (wsUrl.includes('127.0.0.1')) {
|
||||
fallbackUrl = wsUrl.replace('127.0.0.1', 'localhost');
|
||||
}
|
||||
|
||||
if (fallbackUrl && fallbackUrl !== wsUrl) {
|
||||
this.logger(`[WebSocketService] 尝试备用连接: ${fallbackUrl}`);
|
||||
|
||||
// 关闭旧连接
|
||||
if (this.socket) {
|
||||
this.socket.close();
|
||||
}
|
||||
|
||||
// 尝试新连接
|
||||
this.socket = new WebSocket(fallbackUrl);
|
||||
this.setupEventHandlers();
|
||||
}
|
||||
}
|
||||
clearTimeout(fallbackTimeout);
|
||||
}, 3000); // 3秒后如果未连接则尝试备用地址
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger(`[WebSocketService] 连接错误: ${error instanceof Error ? error.message : String(error)}`);
|
||||
this.emit(WebSocketEvent.ERROR, `连接错误: ${error instanceof Error ? error.message : String(error)}`);
|
||||
this.logger(`[WebSocketService] 连接失败: ${error}`);
|
||||
this.emit(WebSocketEvent.ERROR, `连接失败: ${error}`);
|
||||
this.scheduleReconnect();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user