mirror of
https://github.com/Smeagolworms4/MQTT-Explorer.git
synced 2026-09-15 11:03:11 +00:00
44 lines
1.0 KiB
TypeScript
44 lines
1.0 KiB
TypeScript
import { io } from '../server/node_modules/socket.io-client'
|
|
const socket = io(window.location.host , {
|
|
path: window.location.pathname + 'socket.io',
|
|
})
|
|
|
|
const wait = async () => {
|
|
if (!socket.connected) {
|
|
await new Promise(r => setTimeout(r, 50))
|
|
await wait()
|
|
}
|
|
}
|
|
|
|
class IpcSocketIo {
|
|
|
|
public async on(event: string, cb: any) {
|
|
// console.log('IpcSocketIo::on', event, cb)
|
|
await wait()
|
|
socket.on(event, (data: any) => {
|
|
// console.log('Receive Data:', data)
|
|
cb(data)
|
|
})
|
|
}
|
|
|
|
public async removeAllListeners(event: string) {
|
|
// console.log('IpcSocketIo::removeAllListeners', event)
|
|
await wait()
|
|
socket.removeAllListeners(event)
|
|
}
|
|
|
|
public async removeListener(event: string, cb: any) {
|
|
// console.log('IpcSocketIo::removeListener', event, cb)
|
|
await wait()
|
|
socket.removeListener(event, cb)
|
|
}
|
|
|
|
public async send(event: string, data: any) {
|
|
// console.log('IpcSocketIo::send', event, data)
|
|
await wait()
|
|
socket.emit('ipc', [event, data])
|
|
}
|
|
}
|
|
|
|
export const ipcSocketIo = new IpcSocketIo()
|