Created the SocketContext.jsx
This commit is contained in:
parent
e5eb641b34
commit
d0193c3202
32
webui/src/common/contexts/SocketContext/SocketContext.jsx
Normal file
32
webui/src/common/contexts/SocketContext/SocketContext.jsx
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import {createContext, useState} from "react";
|
||||||
|
import {io} from "socket.io-client";
|
||||||
|
|
||||||
|
export const SocketContext = createContext({});
|
||||||
|
|
||||||
|
const URL = process.env.NODE_ENV === "production" ? "https://guessr-api.gnmyt.dev" : "http://localhost:5287";
|
||||||
|
|
||||||
|
export const SocketProvider = ({ children }) => {
|
||||||
|
const socket = io(URL, {autoConnect: false});
|
||||||
|
|
||||||
|
const connect = () => {
|
||||||
|
socket.connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
const send = (event, data) => {
|
||||||
|
socket.emit(event, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
const disconnect = () => {
|
||||||
|
socket.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
const addListener = (event, callback) => {
|
||||||
|
socket.on(event, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<SocketContext.Provider value={{connect, disconnect, send, addListener}}>
|
||||||
|
{children}
|
||||||
|
</SocketContext.Provider>
|
||||||
|
)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user