-
Notifications
You must be signed in to change notification settings - Fork 1
/
WebSocket.ts
27 lines (24 loc) · 853 Bytes
/
WebSocket.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
export interface WebSocketClassOptions {
canUseWebsocket: boolean
previewMode: boolean
log(...a: any[]): void
}
export function createWebSocket({ canUseWebsocket, previewMode, log }: WebSocketClassOptions) {
return class RestrictedWebSocket extends WebSocket {
constructor(url: string | URL, protocols?: string | string[]) {
if (url.toString().toLowerCase().substr(0, 4) !== 'wss:') {
if (previewMode) {
log(
"⚠️ Warning: can't connect to unsafe WebSocket (ws) server in deployed scenes, consider upgrading to wss."
)
} else {
throw new Error("Can't connect to unsafe WebSocket server")
}
}
if (!canUseWebsocket) {
throw new Error("This scene doesn't have allowed to use WebSocket")
}
super(url.toString(), protocols)
}
}
}