-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
55 additions
and
2 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
src/classes/CloudConnection.ts → src/classes/cloud/CloudConnection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type CloudConnection from "./CloudConnection"; | ||
import events from "events"; | ||
import { encode, decode } from "./utils"; | ||
|
||
type OnRequestFn = (name: string, value: string) => void; | ||
|
||
class PacketCloud extends events.EventEmitter { | ||
connection: CloudConnection; | ||
onRequestListeners: OnRequestFn[] = []; | ||
constructor(connection: CloudConnection) { | ||
super(); | ||
this.connection = connection; | ||
this.connection.on("set", (data) => { | ||
if (String(data.name).includes("FROM_USER_")) { | ||
const [name, value] = [...decode(data.value)]; | ||
for(const fn of this.onRequestListeners) fn(name, value); | ||
this.emit(name, value); | ||
} | ||
}); | ||
} | ||
|
||
onRequest(fn: OnRequestFn) { | ||
this.onRequestListeners.push(fn); | ||
return this; | ||
} | ||
|
||
send(name: string, value: string) { | ||
const val = encode(name) + encode(value); | ||
if (val.length > 250) console.warn("Packet length is greater than 250!"); | ||
this.connection.setVariable(`FROM_SERVER_SEND`, val); | ||
} | ||
} | ||
|
||
export default PacketCloud; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export const CHARSET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`~1234567890! @#$%^&*()_-.\";:'?><,/".split(""); | ||
export function encode(string: string) { | ||
let result = ""; | ||
for(let i = 0; i < string.length; i++) { | ||
result += CHARSET.findIndex(x => x === string[i]) + 10; | ||
}; | ||
return result + "00"; | ||
}; | ||
export function* decode(string: string) { | ||
let value = ""; | ||
for(let i = 1; i < string.length; i += 2) { | ||
const idx = Number(string[i-1] + string[i]) - 10; | ||
if(idx < 0) { | ||
yield value; | ||
value = ""; | ||
} else value += CHARSET[idx]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters