pages235/src/customServer.ts
Vitaly Turovsky 05b9f1ac24 initial p2p multiplayer support powered by peerJS
spend many hours fixing weird bugs... still have to few some
2023-09-15 23:34:53 +03:00

34 lines
833 B
TypeScript

import EventEmitter from 'events'
import Client from 'minecraft-protocol/src/client'
window.serverDataChannel ??= {}
export const customCommunication = {
sendData(data) {
//@ts-ignore
window.serverDataChannel[this.isServer ? 'emitClient' : 'emitServer'](data)
},
receiverSetup(processData) {
//@ts-ignore
window.serverDataChannel[this.isServer ? 'emitServer' : 'emitClient'] = (data) => {
processData(data)
}
}
}
export class LocalServer extends EventEmitter.EventEmitter {
socketServer = null
cipher = null
decipher = null
clients = {}
constructor(public version, public customPackets, public hideErrors = false) {
super()
}
listen() {
this.emit('connection', new Client(true, this.version, this.customPackets, this.hideErrors, customCommunication))
}
close() { }
}