small fix x_x
This commit is contained in:
parent
3d11a2871f
commit
7fe48d6993
9 changed files with 77 additions and 76 deletions
|
@ -27,19 +27,26 @@ const allowed_key_to_send = [
|
|||
"F1", "F2", "KeyZ", "KeyX", "KeyC"
|
||||
]
|
||||
|
||||
function connectServer(address: string, name: string) {
|
||||
async function connectServer(address: string, name: string) {
|
||||
player.closeConnection()
|
||||
player.onConnect(name)
|
||||
|
||||
try {
|
||||
let conn = new Connection(address, player.onPacket, (e) => {
|
||||
player.conn = null
|
||||
setServerError(e == null ? "Connection closed due to error" : e)
|
||||
resetWorld()
|
||||
})
|
||||
conn.send(new JoinPacket(name))
|
||||
player.conn = new Connection(
|
||||
await Connection.createSocket(
|
||||
address,
|
||||
(p) => player.onPacket(p),
|
||||
(e) => {
|
||||
player.conn = null
|
||||
setServerError(e == null ? "Connection closed due to error" : e)
|
||||
resetWorld()
|
||||
}
|
||||
)
|
||||
)
|
||||
player.conn.send(new JoinPacket(name))
|
||||
} catch (exception) {
|
||||
setServerError(exception)
|
||||
console.log(exception)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -59,36 +59,27 @@ class VelocityPacket extends Packet {
|
|||
|
||||
class Connection {
|
||||
private socket: WebSocket
|
||||
private on_packet: (packet: Packet) => void
|
||||
private on_close: (error: string | null) => void
|
||||
|
||||
constructor(
|
||||
address: string,
|
||||
static createSocket(address: string,
|
||||
on_packet: (packet: Packet) => void,
|
||||
on_close: (error: string | null) => void
|
||||
): Promise<WebSocket> {
|
||||
return new Promise((resolve, _) => {
|
||||
let socket = new WebSocket(
|
||||
"ws://"+address,
|
||||
"cubic",
|
||||
)
|
||||
socket.onmessage = (e) => on_packet(Packet.fromString(e.data))
|
||||
socket.onclose = (_) => on_close(null)
|
||||
socket.onerror = (e) => on_close(e.toString())
|
||||
socket.onopen = () => resolve(socket)
|
||||
})
|
||||
}
|
||||
|
||||
constructor(
|
||||
socket: WebSocket
|
||||
) {
|
||||
this.socket = new WebSocket(
|
||||
"ws://"+address,
|
||||
"cubic",
|
||||
)
|
||||
this.on_packet = on_packet
|
||||
this.on_close = on_close
|
||||
|
||||
this.socket.onmessage = this._on_message
|
||||
this.socket.onclose = this._on_close
|
||||
this.socket.onerror = this._on_error
|
||||
}
|
||||
|
||||
private _on_message(event: MessageEvent) {
|
||||
this.on_packet(Packet.fromString(event.data))
|
||||
}
|
||||
|
||||
private _on_close(event: CloseEvent) {
|
||||
this.on_close(null)
|
||||
}
|
||||
|
||||
private _on_error(event: Event) {
|
||||
this.on_close(event.toString())
|
||||
this.socket = socket
|
||||
}
|
||||
|
||||
close() {
|
||||
|
@ -96,6 +87,6 @@ class Connection {
|
|||
}
|
||||
|
||||
send(packet: Packet) {
|
||||
this.socket.send(packet.getId()+packet.getData())
|
||||
this.socket.send(packet.getId()+packet.getData().join("\n"))
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue