Page2.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import React, { useEffect } from "react";
  2. import { NavLink } from "react-router-dom";
  3. import { useSocket } from './context/socket';
  4. import logo from './logo.svg';
  5. import './App.css';
  6. const Page2 = () => {
  7. const socket = useSocket();
  8. /* TODO:
  9. 1. Sort how to keep listening the websocket using a callback and put the output on console.log. You can use the useSocket or you can create another way to do so.
  10. 2. send message (from input) to wsocket with SendMessage function
  11. */
  12. useEffect(() => {
  13. /*
  14. // _______________
  15. // SOME IDEAS:
  16. // _______________
  17. // Using hooks?
  18. // Using another way?
  19. // It's up to you, keeping in mind that usually we will transfer short messages but we can also transfer bigger files.
  20. if(socket){
  21. console.log("socket event fired ???");
  22. socket.on("message", (event) => {
  23. console.log("message arrived: ", event.data );
  24. })
  25. }
  26. */
  27. },[]);
  28. const SendMessage = () => {
  29. console.log("sending message..");
  30. }
  31. return (
  32. <div className="App">
  33. <h3 >Page 2</h3>
  34. <header className="App-header">
  35. <NavLink to="/page1" style={{textDecoration:'inherit', color: "inherit"}}>goto Page 1</NavLink>
  36. <br/>
  37. <br/>
  38. <input></input>
  39. <button onClick={SendMessage}>SEND MESSAGE</button>
  40. </header>
  41. </div>
  42. );
  43. }
  44. export default Page2;