feat: upgrade socket.io to v4

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
2022-11-21 23:18:39 +08:00
parent e563e1b507
commit df0d246136
45 changed files with 6585 additions and 1734 deletions

View File

@@ -1,9 +1,13 @@
import { encodePacket } from "./encodePacket"
import { decodePacket } from "./decodePacket"
import encodePacket from "./encodePacket.js"
import decodePacket from "./decodePacket.js"
import { Packet, PacketType, RawData, BinaryType } from "./commons.js"
const SEPARATOR = String.fromCharCode(30) // see https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text
const encodePayload = (packets, callback) => {
const encodePayload = (
packets: Packet[],
callback: (encodedPayload: string) => void
) => {
// some packets may be added to the array while encoding, so the initial length must be saved
const length = packets.length
const encodedPackets = new Array(length)
@@ -20,7 +24,10 @@ const encodePayload = (packets, callback) => {
})
}
const decodePayload = (encodedPayload, binaryType) => {
const decodePayload = (
encodedPayload: string,
binaryType?: BinaryType
): Packet[] => {
const encodedPackets = encodedPayload.split(SEPARATOR)
const packets = []
for (let i = 0; i < encodedPackets.length; i++) {
@@ -33,10 +40,14 @@ const decodePayload = (encodedPayload, binaryType) => {
return packets
}
export default {
protocol: 4,
export const protocol = 4
export {
encodePacket,
encodePayload,
decodePacket,
decodePayload
decodePayload,
Packet,
PacketType,
RawData,
BinaryType
}