Skip to content

Commit 40dbb16

Browse files
committed
fix: #27
1 parent 1fa059f commit 40dbb16

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

packages/socket.io-react-hook/src/useSocket.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ function useSocket<
5555
namespace: typeof namespace === "string" ? namespace : "",
5656
options: typeof namespace === "object" ? namespace : options,
5757
};
58-
const urlConfig = url(opts.namespace, opts.options?.path || "/socket.io");
58+
const urlConfig = url(
59+
opts.namespace,
60+
opts.options?.path || "/socket.io",
61+
opts.options?.port
62+
);
5963
const connectionKey = urlConfig.id;
6064
const namespaceKey = `${connectionKey}${urlConfig.path}`;
6165

@@ -110,7 +114,7 @@ function useSocket<
110114
};
111115
}
112116
return () => {};
113-
}, [enabled]);
117+
}, [enabled, namespaceKey]);
114118

115119
return {
116120
socket: state.current.socket,

packages/socket.io-react-hook/src/utils/url.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ type ParsedUrl = {
3636
export function url(
3737
uri: string | ParsedUrl,
3838
path: string = "",
39-
loc?: Location
39+
defaultPort?: number | string
4040
): ParsedUrl {
4141
let obj = uri as ParsedUrl;
4242

4343
// default to window.location
44-
loc = loc || globalThis.location;
44+
const loc = globalThis.location;
4545
if (null == uri) uri = loc.protocol + "//" + loc.host;
4646

4747
// relative path support
@@ -68,7 +68,9 @@ export function url(
6868

6969
// make sure we treat `localhost:80` and `localhost` equally
7070
if (!obj.port) {
71-
if (/^(http|ws)$/.test(obj.protocol)) {
71+
if (defaultPort) {
72+
obj.port = String(defaultPort);
73+
} else if (/^(http|ws)$/.test(obj.protocol)) {
7274
obj.port = "80";
7375
} else if (/^(http|ws)s$/.test(obj.protocol)) {
7476
obj.port = "443";
@@ -78,7 +80,7 @@ export function url(
7880
obj.path = obj.path || "/";
7981

8082
const ipv6 = obj.host.indexOf(":") !== -1;
81-
const host = ipv6 ? "[" + obj.host + "]" : obj.host;
83+
const host = ipv6 ? "[" + obj.host + "]" : obj.host || "localhost";
8284

8385
// define unique id
8486
obj.id = obj.protocol + "://" + host + ":" + obj.port + path;

0 commit comments

Comments
 (0)