-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnectToRotur.js
95 lines (89 loc) · 2.67 KB
/
connectToRotur.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function connectToWebsocket(server, accounts, username, password) {
// Show the spinner as we begin connecting
showLoadingSpinner();
// Also show a spinner on the submit button
const submitButton = document.querySelector(".modal form button[type='submit']");
submitButton.innerHTML = '<i class="fas fa-spinner fa-spin"></i>';
submitButton.disabled = true;
window.account = undefined;
this.ip = "";
this.username = username;
this.password = password;
this.accounts = accounts;
this.userToken = "";
this.ws = new WebSocket(server);
this.ws.onopen = () => {
// Connection established: hide the spinner
hideLoadingSpinner();
this.ws.send(JSON.stringify({
cmd: "handshake",
val: {
language: "Javascript",
version: {
editorType: "Scratch",
versionNumber: null,
},
},
listener: "handshake_cfg",
}));
this.ws.onmessage = (event) => {
let packet = JSON.parse(event.data);
if (packet.cmd == "client_ip") {
this.ip = packet.val;
} else if (packet.cmd == "pmsg") {
if (packet.val.source_command == "login") {
if ((""+packet.val.payload.username).toLowerCase() === this.username.toLowerCase()) {
this.userToken = packet.val.token;
window.account = packet.val.payload;
console.log("Logged in!");
} else {
console.log("Failed to login!");
submitButton.innerHTML = 'Login';
submitButton.disabled = false;
}
}
}
if (packet.listener === "handshake_cfg") {
let usernameWithPrefix = "ath-" + this.username;
let msg = {
cmd: "setid",
val: usernameWithPrefix,
listener: "set_username_cfg",
};
this.ws.send(JSON.stringify(msg));
}
if (packet.listener == "set_username_cfg") {
let room = "roturTW";
let msg = {
cmd: "link",
val: [room],
listener: "link_cfg",
};
this.ws.send(JSON.stringify(msg));
}
if (packet.listener == "link_cfg") {
this.ws.send(JSON.stringify({
cmd: "pmsg",
val: {
ip: this.ip,
client: {
system: "rotur",
version: "1.0",
},
command: "login",
id: this.userToken,
payload: [this.username, this.password],
},
id: this.accounts,
}));
console.log("Connected!");
}
};
};
this.ws.onclose = () => {
console.log("Disconnected!");
hideLoadingSpinner();
submitButton.innerHTML = 'Login';
submitButton.disabled = false;
};
}