Skip to content

Commit

Permalink
fix(sdk): failed to connect websocket in some cases (#755)
Browse files Browse the repository at this point in the history
  • Loading branch information
easy1090 authored Feb 10, 2025
1 parent 7350c2b commit 2e25f30
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/components/src/utils/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const defaultSocketUrl =
process.env.NODE_ENV === 'development'
? `${socketProtocol}://${location.hostname}:${process.env.LOCAL_CLI_PORT}`
: `${socketProtocol}://${location.host}`;

const ipv4Pattern =
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;

function ensureSocket(socketUrl: string = defaultSocketUrl) {
if (!map.has(socketUrl)) {
const socket = io(socketUrl, {});
Expand Down Expand Up @@ -42,7 +46,9 @@ export function formatURL({
url.port = String(port);
url.hostname = hostname;
url.protocol = location.protocol.includes('https') ? 'wss' : 'ws';
return url.toString();
return ipv4Pattern.test(hostname) || hostname.includes('localhost')
? url.toString()
: `${protocol}//${hostname}`;
}

// compatible with IE11
Expand Down

0 comments on commit 2e25f30

Please sign in to comment.