| 1 | import { connect, Socket as SocketIOSocket } from "socketio-latest"; |
| 2 | |
| 3 | export default class SocketLatest { |
| 4 | socket: SocketIOSocket; |
| 5 | |
| 6 | constructor(serverUrl: string) { |
| 7 | this.socket = connect(serverUrl, { |
| 8 | withCredentials: true, |
| 9 | reconnection: false, |
| 10 | }); |
| 11 | this.socket.on("disconnect", (err) => { |
| 12 | console.log("disconnected from server", err); |
| 13 | }); |
| 14 | this.socket.on("connect", () => { |
| 15 | console.log("connected!!!!"); |
| 16 | }); |
| 17 | } |
| 18 | |
| 19 | disconnect() { |
| 20 | if (this.socket) this.socket.disconnect(); |
| 21 | } |
| 22 | |
| 23 | subscribe(event: string, cb: (error: string | null, msg: any) => void) { |
| 24 | if (!this.socket) return true; |
| 25 | this.socket.on(event, (msg: any) => { |
| 26 | return cb(null, msg); |
| 27 | }); |
| 28 | } |
| 29 | |
| 30 | emit(event: string, data?: any) { |
| 31 | if (this.socket) this.socket.emit(event, data); |
| 32 | } |
| 33 | } |
nothing calls this directly
no outgoing calls
no test coverage detected