-
Notifications
You must be signed in to change notification settings - Fork 1
/
socket.ts
51 lines (39 loc) · 1.09 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
45
46
47
48
49
50
51
import { reactive, Ref } from "vue";
import { io, Socket } from "socket.io-client";
interface EventData {
// Define the structure of your event data here
}
interface AppState {
connected: boolean;
fooEvents: EventData[];
barEvents: EventData[];
}
const state: AppState = reactive({
connected: false,
fooEvents: [],
barEvents: []
});
function initializeSocket(): Socket {
const isProduction: boolean = process.env.NODE_ENV === "production";
const URL: string = isProduction ? "" : import.meta.env.VITE_MICRO_BACKEND_LNK;
const socket: Socket = io(URL, {
extraHeaders: {
"Access-Control-Allow-Origin": "http://localhost:5173", // Allow requests from this origin
},
});
socket.on("connect", () => {
state.connected = true;
});
socket.on("disconnect", () => {
state.connected = false;
});
socket.on("foo", (...args: EventData[]) => {
state.fooEvents.push(...args);
});
socket.on("bar", (...args: EventData[]) => {
state.barEvents.push(...args);
});
return socket;
}
const socket: Socket = initializeSocket();
export { state, socket };