57 lines
1.2 KiB
JavaScript
57 lines
1.2 KiB
JavaScript
|
import io from "socket.io-client";
|
||
|
|
||
|
let client = null;
|
||
|
|
||
|
class SocketIOHelper {
|
||
|
socketUrl = null;
|
||
|
|
||
|
constructor(url) {
|
||
|
if (!url) return;
|
||
|
|
||
|
this.socketUrl = url;
|
||
|
|
||
|
if (client) return;
|
||
|
|
||
|
client = io(url, {
|
||
|
withCredentials: true,
|
||
|
transports: ["websocket", "polling"],
|
||
|
eio: 4,
|
||
|
});
|
||
|
|
||
|
client.on("connect", () => console.log("socket is connected"));
|
||
|
client.on("connect_error", (err) =>
|
||
|
console.log("socket connect error", err)
|
||
|
);
|
||
|
client.on("disconnect", () => console.log("socket is disconnected"));
|
||
|
}
|
||
|
|
||
|
get isEnabled() {
|
||
|
return this.socketUrl !== null;
|
||
|
}
|
||
|
|
||
|
emit = ({ command, data, room = null }) => {
|
||
|
if (!this.isEnabled) return;
|
||
|
|
||
|
if (!client.connected) {
|
||
|
client.on("connect", () => {
|
||
|
room ? client.to(room).emit(command, data) : client.emit(command, data);
|
||
|
});
|
||
|
} else {
|
||
|
room ? client.to(room).emit(command, data) : client.emit(command, data);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
on = (eventName, callback) => {
|
||
|
if (!this.isEnabled) return;
|
||
|
if (!client.connected) {
|
||
|
client.on("connect", () => {
|
||
|
client.on(eventName, callback);
|
||
|
});
|
||
|
} else {
|
||
|
client.on(eventName, callback);
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
export default SocketIOHelper;
|