-
Notifications
You must be signed in to change notification settings - Fork 1
/
socket.ts
44 lines (40 loc) · 1.08 KB
/
socket.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/// <reference no-default-lib="true" />
/// <reference lib="esnext" />
/// <reference lib="dom" />
import type {
Manager,
ManagerOptions,
Socket,
SocketOptions,
} from "./types/socketIO/index.ts";
export type { Manager, ManagerOptions, Socket, SocketOptions };
declare global {
interface Window {
io(
uri: string,
opts?: Partial<ManagerOptions & SocketOptions>,
): Socket;
}
}
const version = "4.2.0";
export async function socketIO() {
const io = await importSocketIO();
return io("https://scrapbox.io", {
reconnectionDelay: 5000,
transports: ["websocket"],
});
}
function importSocketIO(): Promise<Window["io"]> {
const url =
`https://cdnjs.cloudflare.com/ajax/libs/socket.io/${version}/socket.io.min.js`;
if (document.querySelector(`script[src="${url}"]`)) {
return Promise.resolve(window.io);
}
const script = document.createElement("script");
script.src = url;
return new Promise((resolve, reject) => {
script.onload = () => resolve(window.io);
script.onerror = (e) => reject(e);
document.head.append(script);
});
}