Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
TheLazySquid
GitHub Repository: TheLazySquid/GimkitCheat
Path: blob/main/src/network/socketManager.ts
4172 views
1
import { getUnsafeWindow, parseChangePacket } from "../utils";
2
import blueboat from "./blueboat";
3
import * as msgpack from "../../node_modules/colyseus.js/lib/msgpack/index.js"
4
import { Protocol } from "../../node_modules/colyseus.js/dist/colyseus.js";
5
import { writable, get } from "svelte/store";
6
import { onMessage } from "./schemaDecode";
7
8
class SocketManager extends EventTarget {
9
private socket: WebSocket | null = null;
10
transportType = writable<"unknown" | "colyseus" | "blueboat">("unknown");
11
blueboatRoomId: string | null = null;
12
13
setup() {
14
let manager = this;
15
16
// override the default WebSocket
17
class NewWebSocket extends WebSocket {
18
constructor(url: string | URL, params?: string | string[]) {
19
super(url, params)
20
if(!manager.socket) {
21
manager.registerSocket(this);
22
}
23
}
24
25
send(data: any) {
26
manager.onSend(data);
27
super.send(data);
28
}
29
}
30
31
// override XMLHttpRequest to get the room id
32
let nativeXMLSend = XMLHttpRequest.prototype.send;
33
XMLHttpRequest.prototype.send = function() {
34
this.addEventListener('load', () => {
35
if(!this.responseURL.endsWith("/matchmaker/join")) return;
36
let response = JSON.parse(this.responseText);
37
38
manager.blueboatRoomId = response.roomId;
39
console.log("Got Blueboat Room Id: " + manager.blueboatRoomId)
40
})
41
nativeXMLSend.apply(this, arguments);
42
}
43
44
getUnsafeWindow().WebSocket = NewWebSocket;
45
}
46
47
registerSocket(socket: WebSocket) {
48
this.socket = socket;
49
50
// detect the transport type
51
if('Phaser' in getUnsafeWindow()) {
52
this.transportType.set("colyseus");
53
this.addEventListener('colyseusMessage', (e: any) => {
54
if(e.detail.type != "DEVICES_STATES_CHANGES") return;
55
56
let changes = parseChangePacket(e.detail.message);
57
this.dispatchEvent(new CustomEvent('deviceChanges', {
58
detail: changes
59
}))
60
})
61
}
62
else this.transportType.set("blueboat");
63
64
// when we get a message, decode it and dispatch it
65
socket.addEventListener('message', (e) => {
66
let decoded: any;
67
if(get(this.transportType) == 'colyseus') {
68
decoded = onMessage(e);
69
if(!decoded) return;
70
71
this.dispatchEvent(new CustomEvent('colyseusMessage', {
72
detail: decoded
73
}))
74
} else {
75
decoded = blueboat.decode(e.data);
76
if(!decoded) return;
77
78
this.dispatchEvent(new CustomEvent('blueboatMessage', {
79
detail: decoded
80
}))
81
}
82
})
83
}
84
85
onSend(data: any) {
86
// if we're already in a room, get the room id from the data
87
if(get(this.transportType) == "blueboat" && !this.blueboatRoomId) {
88
let decoded = blueboat.decode(data);
89
90
if(decoded.roomId) this.blueboatRoomId = decoded.roomId;
91
if(decoded.room) this.blueboatRoomId = decoded.room;
92
93
if(this.blueboatRoomId) {
94
console.log("Got Blueboat Room Id: " + this.blueboatRoomId)
95
}
96
}
97
}
98
99
sendMessage(channel: string, data: any) {
100
if(!this.socket) return;
101
if(!this.blueboatRoomId && get(this.transportType) == "blueboat") return;
102
103
let encoded: any;
104
if(get(this.transportType) == 'colyseus') {
105
let header = [Protocol.ROOM_DATA]
106
let channelEncoded = msgpack.encode(channel)
107
let packetEncoded = msgpack.encode(data)
108
109
// combine the arraybuffers
110
encoded = new Uint8Array(channelEncoded.byteLength + packetEncoded.byteLength + header.length)
111
encoded.set(header)
112
encoded.set(new Uint8Array(channelEncoded), header.length)
113
encoded.set(new Uint8Array(packetEncoded), header.length + channelEncoded.byteLength)
114
}
115
else encoded = blueboat.encode(channel, data, this.blueboatRoomId);
116
117
this.socket.send(encoded);
118
}
119
}
120
121
const socketManager = new SocketManager();
122
export default socketManager;
123