|
1 | | -import {Chat} from './components/chat'; |
2 | | -import 'bootstrap/dist/css/bootstrap.min.css'; |
| 1 | +import { useState } from "react"; |
| 2 | +import Chat from "./components/chat"; |
| 3 | +import "./index.css"; |
| 4 | +import SockJS from "sockjs-client"; |
| 5 | +import { over } from "stompjs"; |
| 6 | +import RegisterScreen from "./components/RegisterScreen"; |
| 7 | +import ChatScreen from "./components/ChatScreen"; |
| 8 | + |
| 9 | +let stomClient = null; |
| 10 | + |
3 | 11 | function App() { |
| 12 | + const [connected, setConnected] = useState(false); |
| 13 | + const [name, setName] = useState(""); |
| 14 | + const [publicRoom, setPublicRoom] = useState([]); |
| 15 | + const [privateRoom, setPrivateRoom] = useState(new Map()); |
| 16 | + |
| 17 | + function register(e) { |
| 18 | + e.preventDefault(); |
| 19 | + const Sock = new SockJS("http://localhost:8080/ws"); |
| 20 | + stomClient = over(Sock); |
| 21 | + stomClient.connect({}, onConnected, (err) => |
| 22 | + alert(`An error has occurred: ${err}`) |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + function onConnected() { |
| 27 | + setConnected(true); |
| 28 | + stomClient.subscribe("/chat/public", onPublic); |
| 29 | + stomClient.subscribe("/user/" + name + "/private", onPrivate); |
| 30 | + stomClient.send( |
| 31 | + "/app/message", |
| 32 | + {}, |
| 33 | + JSON.stringify({ author: name, status: "JOIN" }) |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + function onPublic(payload) { |
| 38 | + console.log(payload); |
| 39 | + let payloadData = JSON.parse(payload.body); |
| 40 | + if (payload.status === "JOIN") { |
| 41 | + if (!privateRoom.get(payloadData.senderName)) { |
| 42 | + privateRoom.set(payloadData.senderName, []); |
| 43 | + setPrivateRoom(new Map(privateRoom)); |
| 44 | + } |
| 45 | + } else { |
| 46 | + publicRoom.push(payloadData); |
| 47 | + setPublicRoom([...publicRoom]); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + function onPrivate(payload) { |
| 52 | + let payloadData = JSON.parse(payload); |
| 53 | + if (privateRoom.get(payloadData.name)) { |
| 54 | + let data = []; |
| 55 | + data.push(payloadData); |
| 56 | + privateRoom.set(payloadData.name, data); |
| 57 | + setPrivateRoom(new Map(privateRoom)); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + function sendMessage(message) { |
| 62 | + if (stomClient) { |
| 63 | + var chatMsg = { |
| 64 | + author: name, |
| 65 | + message: message, |
| 66 | + status: "MESSAGE", |
| 67 | + }; |
| 68 | + stomClient.send("/app/message", {}, JSON.stringify(chatMsg)); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if (!connected) { |
| 73 | + return <RegisterScreen name={name} setName={setName} register={register} />; |
| 74 | + } |
| 75 | + |
4 | 76 | return ( |
5 | | - <div className="App"> |
6 | | - <Chat/> |
7 | | - </div> |
| 77 | + <ChatScreen |
| 78 | + messages={publicRoom.filter((msg) => msg.message !== null)} |
| 79 | + sendMessage={sendMessage} |
| 80 | + user={name} |
| 81 | + /> |
8 | 82 | ); |
9 | 83 | } |
10 | 84 |
|
|
0 commit comments