diff options
author | dittonjs <jditton.atomic@gmail.com> | 2022-03-08 18:25:58 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-08 18:25:58 -0700 |
commit | 5e2996c0fdeee2eaa88a72f79d2e0d600edeb5a6 (patch) | |
tree | 3e9cd6e10f81a65f76478910f6feb1b89d6ce1dc /client/components | |
parent | 15800f84a42b0aa53311ff88fa3d554653a2511a (diff) | |
parent | 1b7c6ee006b50ce29c9cb3ee42fad29b7db0f6f3 (diff) | |
download | locchat-5e2996c0fdeee2eaa88a72f79d2e0d600edeb5a6.tar.gz locchat-5e2996c0fdeee2eaa88a72f79d2e0d600edeb5a6.zip |
Merge pull request #4 from dittonjs/jd/add-websockets
Jd/add websockets
Diffstat (limited to 'client/components')
-rw-r--r-- | client/components/home/_home.jsx | 4 | ||||
-rw-r--r-- | client/components/home/ping.jsx | 84 |
2 files changed, 88 insertions, 0 deletions
diff --git a/client/components/home/_home.jsx b/client/components/home/_home.jsx index 4b1ab46..405a968 100644 --- a/client/components/home/_home.jsx +++ b/client/components/home/_home.jsx @@ -4,6 +4,7 @@ import { ApiContext } from '../../utils/api_context'; import { AuthContext } from '../../utils/auth_context'; import { RolesContext } from '../../utils/roles_context'; import { Button } from '../common/button'; +import { Ping } from './ping'; export const Home = () => { const [, setAuthToken] = useContext(AuthContext); @@ -42,6 +43,9 @@ export const Home = () => { Admin </Button> )} + <section> + <Ping /> + </section> </div> ); }; diff --git a/client/components/home/ping.jsx b/client/components/home/ping.jsx new file mode 100644 index 0000000..6166921 --- /dev/null +++ b/client/components/home/ping.jsx @@ -0,0 +1,84 @@ +import { useState, useEffect, useRef, useContext } from 'react'; +import { Button } from '../common/button'; +import { io } from 'socket.io-client'; +import { AuthContext } from '../../utils/auth_context'; + +export const Ping = () => { + const [pings, setPings] = useState([]); + const [key, setKey] = useState('defaultkey'); + const [currentRoom, setCurrentRoom] = useState(null); + const [authToken] = useContext(AuthContext); + const [socket, setSocket] = useState(null); + + useEffect(() => { + // instantiates a socket object and initiates the connection... + // you probably want to make sure you are only doing this in one component at a time. + const socket = io({ + auth: { token: authToken }, + query: { message: 'I am the query ' }, + }); + + // adds an event listener to the connection event + socket.on('connect', () => { + setSocket(socket); + }); + + // adds event listener to the disconnection event + socket.on('disconnect', () => { + console.log('Disconnected'); + }); + + // recieved a pong event from the server + socket.on('pong', (data) => { + console.log('Recieved pong', data); + }); + + // IMPORTANT! Unregister from all events when the component unmounts and disconnect. + return () => { + socket.off('connect'); + socket.off('disconnect'); + socket.off('pong'); + socket.disconnect(); + }; + }, []); + + useEffect(() => { + // if our token changes we need to tell the socket also + if (socket) { + // this is a little weird because we are modifying this object in memory + // i dunno a better way to do this though... + socket.auth.token = authToken; + } + }, [authToken]); + + if (!socket) return 'Loading...'; + + const sendPing = () => { + // sends a ping to the server to be broadcast to everybody in the room + currentRoom && socket.emit('ping', { currentRoom }); + }; + + const joinRoom = () => { + // tells the server to remove the current client from the current room and add them to the new room + socket.emit('join-room', { currentRoom, newRoom: key }, (response) => { + console.log(response); + setCurrentRoom(response.room); + }); + }; + + return ( + <> + <header>Ping: {currentRoom || '(No room joined)'}</header> + <section> + <input + type="text" + className="border-2 border-gray-700 p-2 rounded" + value={key} + onChange={(e) => setKey(e.target.value)} + /> + <Button onClick={joinRoom}>Connect To Room</Button> + <Button onClick={sendPing}>Send Ping</Button> + </section> + </> + ); +}; |