update socket.ts

This commit is contained in:
Chen, Tai-Lin committed 2024-03-04 23:35:05 +08:00
1 parent 4739849549
commit af9b443865
1 file changed
+11 -7
@@ -1,13 +1,15 @@
import io from 'socket.io-client' import io from 'socket.io-client'
class SocketWrapper { class SocketWrapper {
socket: any
listeners: any
constructor() { constructor() {
this.socket = null this.socket = null
this.listeners = [] this.listeners = []
} }
connect(session) { connect(session: any) {
return new Promise((resolve, reject) => { return new Promise<void>((resolve, reject) => {
this.disconnect() this.disconnect()
this.socket = io('', { this.socket = io('', {
@@ -28,7 +30,7 @@ class SocketWrapper {
}) })
} }
on(event, callback) { on(event: any, callback: any) {
this.listeners.push({ event, callback }) this.listeners.push({ event, callback })
if (this.socket) { if (this.socket) {
this.socket.on(event, callback) this.socket.on(event, callback)
@@ -36,12 +38,14 @@ class SocketWrapper {
} }
bindListeners() { bindListeners() {
this.listeners.forEach(({ event, callback }) => { this.listeners.forEach(
this.socket.on(event, callback) ({ event, callback }: { event: any; callback: any }) => {
}) this.socket.on(event, callback)
}
)
} }
emit(event, data) { emit(event: any, data: any) {
if (!this.socket) { if (!this.socket) {
console.warn('Socket is not connected!') console.warn('Socket is not connected!')
return return