From e9453ba47fa8fe19fb15b5e7ce0cde3addedc867 Mon Sep 17 00:00:00 2001 From: Choco <94597336+ChocoMeow@users.noreply.github.com> Date: Sat, 24 Jun 2023 17:52:00 +0800 Subject: [PATCH] Fixed typo --- web/static/js/socket.js | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 web/static/js/socket.js diff --git a/web/static/js/socket.js b/web/static/js/socket.js new file mode 100644 index 0000000..2ed2aa5 --- /dev/null +++ b/web/static/js/socket.js @@ -0,0 +1,47 @@ +class Socket { + constructor(url) { + this.url = url; + this.socket = null; + } + + connect(player) { + this.socket = io.connect(this.url); + + this.socket.on('connect', () => { + console.log("Connected to server!"); + }); + + this.socket.on('disconnect', () => { + player.init(); + console.log("Disconnected from server!"); + }); + + this.socket.on('error', (e) => { + console.log(e); + }) + } + + disconnect() { + if (this.socket) { + this.socket.disconnect(); + } + } + + send(msg) { + if (this.socket) { + this.socket.emit('message', msg); + } + } + + addMessageListener(callback) { + if (this.socket) { + this.socket.on('message', callback); + } + } + + removeMessageListener(callback) { + if (this.socket) { + this.socket.off('message', callback); + } + } +} \ No newline at end of file