fix: use 127.0.0.1 for WebSocket when host is localhost/::1 to avoid IPv6 fallback delay (~300ms)

This commit is contained in:
2026-08-26 22:45:56 +07:00
parent fa01ef02f7
commit 9ccc28d211
3 changed files with 19 additions and 5 deletions
+12 -2
View File
@@ -1853,8 +1853,14 @@ async function fxRtStart() {
fxRt.node = new AudioWorkletNode(ctx, 'sf-fx-realtime', { numberOfInputs: 1, numberOfOutputs: 1, outputChannelCount: [2] });
const u = new URL(API_BASE_URL);
const wsProto = u.protocol === 'https:' ? 'wss://' : 'ws://';
// WS to 'localhost' bi tre ~300ms: Chromium thu ::1 truoc (Windows khong
// co listener IPv6 -> SYN hang ~2s) roi moi fallback 127.0.0.1. Dung
// thang 127.0.0.1 (loopback) -> WS open ~5ms. Server chap nhan loopback.
const wsHost = (u.hostname === 'localhost' || u.hostname === '::1')
? '127.0.0.1' + (u.port ? ':' + u.port : '')
: u.host;
const tok = localStorage.getItem('sonic_token') || '';
const wsUrl = `${wsProto}${u.host}${st.ws_url}${tok ? '?token=' + encodeURIComponent(tok) : ''}`;
const wsUrl = `${wsProto}${wsHost}${st.ws_url}${tok ? '?token=' + encodeURIComponent(tok) : ''}`;
const ws = new WebSocket(wsUrl);
fxRt.ws = ws;
ws.binaryType = 'arraybuffer';
@@ -24501,8 +24507,12 @@ const App = () => {
entry.node = node;
const u = new URL(API_BASE_URL);
const wsProto = u.protocol === 'https:' ? 'wss://' : 'ws://';
// WS to 'localhost' bi tre ~300ms (Chromium thu ::1 truoc roi fallback) - dung 127.0.0.1
const wsHost = (u.hostname === 'localhost' || u.hostname === '::1')
? '127.0.0.1' + (u.port ? ':' + u.port : '')
: u.host;
const tok = localStorage.getItem('sonic_token') || '';
const ws = new WebSocket(`${wsProto}${u.host}${st.ws_url}${tok ? '?token=' + encodeURIComponent(tok) : ''}`);
const ws = new WebSocket(`${wsProto}${wsHost}${st.ws_url}${tok ? '?token=' + encodeURIComponent(tok) : ''}`);
entry.ws = ws;
ws.binaryType = 'arraybuffer';
ws.onopen = () => {