├── .gitignore ├── README.md ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── server.js ├── src ├── App.js ├── components │ ├── Chat.jsx │ └── JoinBlock.jsx ├── index.css ├── index.js ├── reducer.js └── socket.js └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 2 | 3 | ## Available Scripts 4 | 5 | In the project directory, you can run: 6 | 7 | ### `yarn start` 8 | 9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 11 | 12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console. 14 | 15 | ### `yarn test` 16 | 17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 19 | 20 | ### `yarn build` 21 | 22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance. 24 | 25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed! 27 | 28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 29 | 30 | ### `yarn eject` 31 | 32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 33 | 34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 35 | 36 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 37 | 38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 39 | 40 | ## Learn More 41 | 42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 43 | 44 | To learn React, check out the [React documentation](https://reactjs.org/). 45 | 46 | ### Code Splitting 47 | 48 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting 49 | 50 | ### Analyzing the Bundle Size 51 | 52 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size 53 | 54 | ### Making a Progressive Web App 55 | 56 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app 57 | 58 | ### Advanced Configuration 59 | 60 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration 61 | 62 | ### Deployment 63 | 64 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment 65 | 66 | ### `yarn build` fails to minify 67 | 68 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify 69 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "chat-lite", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^4.2.4", 7 | "@testing-library/react": "^9.3.2", 8 | "@testing-library/user-event": "^7.1.2", 9 | "axios": "^0.19.2", 10 | "express": "^4.17.1", 11 | "nodemon": "^2.0.3", 12 | "react": "^16.13.1", 13 | "react-dom": "^16.13.1", 14 | "react-scripts": "3.4.1", 15 | "socket.io": "^2.3.0", 16 | "socket.io-client": "^2.3.0" 17 | }, 18 | "scripts": { 19 | "start": "react-scripts start", 20 | "build": "react-scripts build", 21 | "test": "react-scripts test", 22 | "eject": "react-scripts eject", 23 | "server": "nodemon server.js" 24 | }, 25 | "eslintConfig": { 26 | "extends": "react-app" 27 | }, 28 | "proxy": "http://localhost:9999", 29 | "browserslist": { 30 | "production": [ 31 | ">0.2%", 32 | "not dead", 33 | "not op_mini all" 34 | ], 35 | "development": [ 36 | "last 1 chrome version", 37 | "last 1 firefox version", 38 | "last 1 safari version" 39 | ] 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Archakov06/react-simple-chat/c0478e23960d5f66c99001e47d9f501b8a73fcc3/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 14 | 15 | 21 | 30 | React App 31 | 32 | 33 | 34 |
35 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Archakov06/react-simple-chat/c0478e23960d5f66c99001e47d9f501b8a73fcc3/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Archakov06/react-simple-chat/c0478e23960d5f66c99001e47d9f501b8a73fcc3/public/logo512.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | 3 | const app = express(); 4 | const server = require('http').Server(app); 5 | const io = require('socket.io')(server); 6 | 7 | app.use(express.json()); 8 | 9 | const rooms = new Map(); 10 | 11 | app.get('/rooms/:id', (req, res) => { 12 | const { id: roomId } = req.params; 13 | const obj = rooms.has(roomId) 14 | ? { 15 | users: [...rooms.get(roomId).get('users').values()], 16 | messages: [...rooms.get(roomId).get('messages').values()], 17 | } 18 | : { users: [], messages: [] }; 19 | res.json(obj); 20 | }); 21 | 22 | app.post('/rooms', (req, res) => { 23 | const { roomId, userName } = req.body; 24 | if (!rooms.has(roomId)) { 25 | rooms.set( 26 | roomId, 27 | new Map([ 28 | ['users', new Map()], 29 | ['messages', []], 30 | ]), 31 | ); 32 | } 33 | res.send(); 34 | }); 35 | 36 | io.on('connection', (socket) => { 37 | socket.on('ROOM:JOIN', ({ roomId, userName }) => { 38 | socket.join(roomId); 39 | rooms.get(roomId).get('users').set(socket.id, userName); 40 | const users = [...rooms.get(roomId).get('users').values()]; 41 | socket.to(roomId).broadcast.emit('ROOM:SET_USERS', users); 42 | }); 43 | 44 | socket.on('ROOM:NEW_MESSAGE', ({ roomId, userName, text }) => { 45 | const obj = { 46 | userName, 47 | text, 48 | }; 49 | rooms.get(roomId).get('messages').push(obj); 50 | socket.to(roomId).broadcast.emit('ROOM:NEW_MESSAGE', obj); 51 | }); 52 | 53 | socket.on('disconnect', () => { 54 | rooms.forEach((value, roomId) => { 55 | if (value.get('users').delete(socket.id)) { 56 | const users = [...value.get('users').values()]; 57 | socket.to(roomId).broadcast.emit('ROOM:SET_USERS', users); 58 | } 59 | }); 60 | }); 61 | 62 | console.log('user connected', socket.id); 63 | }); 64 | 65 | server.listen(9999, (err) => { 66 | if (err) { 67 | throw Error(err); 68 | } 69 | console.log('Сервер запущен!'); 70 | }); 71 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import axios from 'axios'; 3 | 4 | import socket from './socket'; 5 | 6 | import reducer from './reducer'; 7 | import JoinBlock from './components/JoinBlock'; 8 | import Chat from './components/Chat'; 9 | 10 | function App() { 11 | const [state, dispatch] = React.useReducer(reducer, { 12 | joined: false, 13 | roomId: null, 14 | userName: null, 15 | users: [], 16 | messages: [], 17 | }); 18 | 19 | const onLogin = async (obj) => { 20 | dispatch({ 21 | type: 'JOINED', 22 | payload: obj, 23 | }); 24 | socket.emit('ROOM:JOIN', obj); 25 | const { data } = await axios.get(`/rooms/${obj.roomId}`); 26 | dispatch({ 27 | type: 'SET_DATA', 28 | payload: data, 29 | }); 30 | }; 31 | 32 | const setUsers = (users) => { 33 | dispatch({ 34 | type: 'SET_USERS', 35 | payload: users, 36 | }); 37 | }; 38 | 39 | const addMessage = (message) => { 40 | dispatch({ 41 | type: 'NEW_MESSAGE', 42 | payload: message, 43 | }); 44 | }; 45 | 46 | React.useEffect(() => { 47 | socket.on('ROOM:SET_USERS', setUsers); 48 | socket.on('ROOM:NEW_MESSAGE', addMessage); 49 | }, []); 50 | 51 | window.socket = socket; 52 | 53 | return ( 54 |
55 | {!state.joined ? ( 56 | 57 | ) : ( 58 | 59 | )} 60 |
61 | ); 62 | } 63 | 64 | export default App; 65 | -------------------------------------------------------------------------------- /src/components/Chat.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import socket from '../socket'; 3 | 4 | function Chat({ users, messages, userName, roomId, onAddMessage }) { 5 | const [messageValue, setMessageValue] = React.useState(''); 6 | const messagesRef = React.useRef(null); 7 | 8 | const onSendMessage = () => { 9 | socket.emit('ROOM:NEW_MESSAGE', { 10 | userName, 11 | roomId, 12 | text: messageValue, 13 | }); 14 | onAddMessage({ userName, text: messageValue }); 15 | setMessageValue(''); 16 | }; 17 | 18 | React.useEffect(() => { 19 | messagesRef.current.scrollTo(0, 99999); 20 | }, [messages]); 21 | 22 | return ( 23 |
24 |
25 | Комната: {roomId} 26 |
27 | Онлайн ({users.length}): 28 | 33 |
34 |
35 |
36 | {messages.map((message) => ( 37 |
38 |

{message.text}

39 |
40 | {message.userName} 41 |
42 |
43 | ))} 44 |
45 |
46 | 51 | 54 |
55 |
56 |
57 | ); 58 | } 59 | 60 | export default Chat; 61 | -------------------------------------------------------------------------------- /src/components/JoinBlock.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import axios from 'axios'; 3 | 4 | function JoinBlock({ onLogin }) { 5 | const [roomId, setRoomId] = React.useState(''); 6 | const [userName, setUserName] = React.useState(''); 7 | const [isLoading, setLoading] = React.useState(false); 8 | 9 | const onEnter = async () => { 10 | if (!roomId || !userName) { 11 | return alert('Неверные данные'); 12 | } 13 | const obj = { 14 | roomId, 15 | userName, 16 | }; 17 | setLoading(true); 18 | await axios.post('/rooms', obj); 19 | onLogin(obj); 20 | }; 21 | 22 | return ( 23 |
24 | setRoomId(e.target.value)} 29 | /> 30 | setUserName(e.target.value)} 35 | /> 36 | 39 |
40 | ); 41 | } 42 | 43 | export default JoinBlock; 44 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | * { 2 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 3 | 'Open Sans', 'Helvetica Neue', sans-serif; 4 | outline: none; 5 | color: black; 6 | } 7 | 8 | .wrapper { 9 | max-width: 40%; 10 | margin: 100px auto; 11 | min-width: 720px; 12 | } 13 | 14 | .join-block { 15 | margin: 0 auto; 16 | width: 300px; 17 | } 18 | 19 | .join-block input { 20 | font-size: 24px; 21 | width: 100%; 22 | height: 50px; 23 | padding-bottom: 5px; 24 | padding-left: 10px; 25 | margin-bottom: 10px; 26 | } 27 | 28 | .join-block button { 29 | width: 100%; 30 | padding: 10px; 31 | margin-top: 10px; 32 | font-weight: bold; 33 | cursor: pointer; 34 | } 35 | 36 | .chat { 37 | display: flex; 38 | height: 500px; 39 | border: 1px solid rgba(159, 183, 197, 0.2); 40 | border-radius: 8px; 41 | overflow: hidden; 42 | } 43 | 44 | .chat-users { 45 | border-right: 1px solid rgba(159, 183, 197, 0.1); 46 | padding: 20px; 47 | width: 200px; 48 | background-color: #f6f9fa; 49 | } 50 | 51 | .chat-messages { 52 | display: flex; 53 | flex-direction: column; 54 | justify-content: space-between; 55 | flex: 1; 56 | padding: 30px; 57 | } 58 | 59 | .messages { 60 | flex: 1; 61 | height: calc(100% - 155px); 62 | overflow: auto; 63 | } 64 | 65 | .message { 66 | margin-bottom: 20px; 67 | } 68 | 69 | .message p { 70 | display: inline-flex; 71 | border-radius: 10px; 72 | border-top: 1px solid rgba(0, 0, 0, 0.1); 73 | background-color: #7160ff; 74 | padding: 10px 15px 15px; 75 | color: #fff; 76 | margin-bottom: 2px; 77 | } 78 | 79 | .message span { 80 | opacity: 0.5; 81 | font-size: 14px; 82 | } 83 | 84 | .chat-users ul { 85 | list-style: none; 86 | padding: 0; 87 | margin: 0; 88 | } 89 | 90 | .chat-users ul li { 91 | margin-top: 10px; 92 | border-radius: 3px; 93 | background-color: #fff; 94 | padding: 8px 14px; 95 | box-shadow: 0 3px 5px rgba(159, 183, 197, 0.1); 96 | font-weight: 500; 97 | } 98 | 99 | .chat-messages form { 100 | margin-top: 20px; 101 | padding-top: 20px; 102 | border-top: 1px solid rgba(0, 0, 0, 0.1); 103 | } 104 | 105 | .chat-messages form textarea { 106 | width: 100%; 107 | margin-bottom: 10px; 108 | } 109 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | 4 | import App from './App'; 5 | 6 | import './index.css'; 7 | 8 | ReactDOM.render( 9 | 10 | 11 | , 12 | document.getElementById('root'), 13 | ); 14 | -------------------------------------------------------------------------------- /src/reducer.js: -------------------------------------------------------------------------------- 1 | export default (state, action) => { 2 | switch (action.type) { 3 | case 'JOINED': 4 | return { 5 | ...state, 6 | joined: true, 7 | userName: action.payload.userName, 8 | roomId: action.payload.roomId, 9 | }; 10 | 11 | case 'SET_DATA': 12 | return { 13 | ...state, 14 | users: action.payload.users, 15 | messages: action.payload.messages, 16 | }; 17 | 18 | case 'SET_USERS': 19 | return { 20 | ...state, 21 | users: action.payload, 22 | }; 23 | 24 | case 'NEW_MESSAGE': 25 | return { 26 | ...state, 27 | messages: [...state.messages, action.payload], 28 | }; 29 | 30 | default: 31 | return state; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /src/socket.js: -------------------------------------------------------------------------------- 1 | import io from 'socket.io-client'; 2 | 3 | const socket = io(); 4 | 5 | export default socket; 6 | --------------------------------------------------------------------------------