From d0193c32029758ca118a68dea2270b12bb64a78b Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Sat, 10 Jun 2023 03:56:13 +0200 Subject: [PATCH] Created the SocketContext.jsx --- .../contexts/SocketContext/SocketContext.jsx | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 webui/src/common/contexts/SocketContext/SocketContext.jsx diff --git a/webui/src/common/contexts/SocketContext/SocketContext.jsx b/webui/src/common/contexts/SocketContext/SocketContext.jsx new file mode 100644 index 0000000..682733e --- /dev/null +++ b/webui/src/common/contexts/SocketContext/SocketContext.jsx @@ -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 ( + + {children} + + ) +} \ No newline at end of file