mirror of
https://github.com/bolucat/Archive.git
synced 2025-09-26 20:21:35 +08:00
21 lines
563 B
TypeScript
21 lines
563 B
TypeScript
import { baseURL } from "@/utils/constants";
|
|
import { removePrefix } from "./utils";
|
|
|
|
const ssl = window.location.protocol === "https:";
|
|
const protocol = ssl ? "wss:" : "ws:";
|
|
|
|
export default function command(
|
|
url: string,
|
|
command: string,
|
|
onmessage: WebSocket["onmessage"],
|
|
onclose: WebSocket["onclose"]
|
|
) {
|
|
url = removePrefix(url);
|
|
url = `${protocol}//${window.location.host}${baseURL}/api/command${url}`;
|
|
|
|
const conn = new window.WebSocket(url);
|
|
conn.onopen = () => conn.send(command);
|
|
conn.onmessage = onmessage;
|
|
conn.onclose = onclose;
|
|
}
|