Jeongmin Seo

merge feature/chat branch

1 +# Logs
2 +logs
3 +*.log
4 +npm-debug.log*
5 +yarn-debug.log*
6 +yarn-error.log*
7 +lerna-debug.log*
8 +.pnpm-debug.log*
9 +
10 +# Diagnostic reports (https://nodejs.org/api/report.html)
11 +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
12 +
13 +# Runtime data
14 +pids
15 +*.pid
16 +*.seed
17 +*.pid.lock
18 +
19 +# Directory for instrumented libs generated by jscoverage/JSCover
20 +lib-cov
21 +
22 +# Coverage directory used by tools like istanbul
23 +coverage
24 +*.lcov
25 +
26 +# nyc test coverage
27 +.nyc_output
28 +
29 +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
30 +.grunt
31 +
32 +# Bower dependency directory (https://bower.io/)
33 +bower_components
34 +
35 +# node-waf configuration
36 +.lock-wscript
37 +
38 +# Compiled binary addons (https://nodejs.org/api/addons.html)
39 +build/Release
40 +
41 +# Dependency directories
42 +**node_modules/
43 +jspm_packages/
44 +
45 +# Snowpack dependency directory (https://snowpack.dev/)
46 +web_modules/
47 +
48 +# TypeScript cache
49 +*.tsbuildinfo
50 +
51 +# Optional npm cache directory
52 +.npm
53 +
54 +# Optional eslint cache
55 +.eslintcache
56 +
57 +# Microbundle cache
58 +.rpt2_cache/
59 +.rts2_cache_cjs/
60 +.rts2_cache_es/
61 +.rts2_cache_umd/
62 +
63 +# Optional REPL history
64 +.node_repl_history
65 +
66 +# Output of 'npm pack'
67 +*.tgz
68 +
69 +# Yarn Integrity file
70 +.yarn-integrity
71 +
72 +# dotenv environment variables file
73 +.env
74 +.env.test
75 +.env.production
76 +
77 +# parcel-bundler cache (https://parceljs.org/)
78 +.cache
79 +.parcel-cache
80 +
81 +# Next.js build output
82 +.next
83 +out
84 +
85 +# Nuxt.js build / generate output
86 +.nuxt
87 +dist
88 +
89 +# Gatsby files
90 +.cache/
91 +# Comment in the public line in if your project uses Gatsby and not Next.js
92 +# https://nextjs.org/blog/next-9-1#public-directory-support
93 +# public
94 +
95 +# vuepress build output
96 +.vuepress/dist
97 +
98 +# Serverless directories
99 +.serverless/
100 +
101 +# FuseBox cache
102 +.fusebox/
103 +
104 +# DynamoDB Local files
105 +.dynamodb/
106 +
107 +# TernJS port file
108 +.tern-port
109 +
110 +# Stores VSCode versions used for testing VSCode extensions
111 +.vscode-test
112 +
113 +# yarn v2
114 +.yarn/cache
115 +.yarn/unplugged
116 +.yarn/build-state.yml
117 +.yarn/install-state.gz
118 +.pnp.*
119 +
120 +#보안관련
121 +.package-lock.json
...\ No newline at end of file ...\ No newline at end of file
1 +const express = require("express");
2 +const http = require("http");
3 +const app = express();
4 +const path = require("path")
5 +const server = http.createServer(app);
6 +const socketIO = require("socket.io")
7 +const moment = require("moment")
8 +
9 +
10 +const io = socketIO(server);
11 +
12 +app.use(express.static(path.join(__dirname, "src")))
13 +const PORT = process.env.PORT || 3000;
14 +
15 +io.on('connection', (socket) => {
16 + socket.on("chatting", (data)=>{
17 + const { name, msg } = data;
18 + io.emit("chatting", {
19 + name,
20 + msg,
21 + time: moment(new Date()).format("h:ss A")
22 + })
23 + })
24 +});
25 +
26 +server.listen(PORT, ()=>console.log(`server is running ${PORT}`))
...\ No newline at end of file ...\ No newline at end of file
This diff is collapsed. Click to expand it.
1 +{
2 + "name": "chat",
3 + "version": "1.0.0",
4 + "description": "",
5 + "main": "index.js",
6 + "scripts": {
7 + "start": "nodemon ./app.js",
8 + "test": "echo \"Error: no test specified\" && exit 1"
9 + },
10 + "keywords": [],
11 + "author": "",
12 + "license": "ISC",
13 + "dependencies": {
14 + "dayjs": "^1.11.2",
15 + "express": "^4.18.1",
16 + "moment": "^2.29.3",
17 + "socket.io": "^4.5.1"
18 + }
19 +}
1 +* {
2 + margin: 0;
3 + padding: 0;
4 +}
5 +
6 +html, body {
7 + height : 100%;
8 +}
9 +
10 +.wrapper {
11 + height : 100%;
12 + width: 100%;
13 + display: flex;
14 + flex-direction: column;
15 + overflow: hidden;
16 +}
17 +
18 +.user-container {
19 + background: rebeccapurple;
20 + flex: 1;
21 + display: flex;
22 + justify-content: flex-start;
23 + align-items: center;
24 + padding: 0.5rem;
25 +}
26 +
27 +.user-container .nickname {
28 + font-size : 14px;
29 + margin-right : 1.5rem;
30 + margin-left : 1rem;
31 + color:#fff;
32 +}
33 +
34 +.user-container input {
35 + border-radius: 3px;
36 + border: none;
37 + height: 80%;
38 +}
39 +
40 +.display-container {
41 + background: #D2D2FF;
42 + flex : 12;
43 + overflow-y:scroll;
44 +}
45 +
46 +.input-container {
47 + flex:1;
48 + display:flex;
49 + justify-content: stretch;
50 + align-items: stretch;
51 +}
52 +
53 +.input-container span {
54 + display: flex;
55 + justify-content: flex-start;
56 + align-items:center;
57 + padding: 0.3rem;
58 + width: 100%;
59 +}
60 +
61 +.chatting-input {
62 + font-size:12px;
63 + height:100%;
64 + flex:8;
65 + border:none;
66 +}
67 +
68 +.send-button {
69 + flex:1;
70 + background: rebeccapurple;
71 + color:#fff;
72 + border:none;
73 + height:100%;
74 + border-radius:3px;
75 +}
76 +
77 +.chatting-list li {
78 + width:50%;
79 + padding:0.3rem;
80 + display:flex;
81 + justify-content: flex-start;
82 + align-items:flex-end;
83 + margin-top:0.5rem;
84 +}
85 +
86 +.profile {
87 + display: flex;
88 + flex-direction: column;
89 + align-items: center;
90 + justify-content: center;
91 + flex: 1;
92 +}
93 +
94 +.profile .user {
95 + font-size: 10px;
96 + margin-bottom: 0.3rem;
97 +}
98 +
99 +.profile .image {
100 + border-radius: 50%;
101 + object-fit: cover;
102 + width: 50px;
103 + height: 50px;
104 +}
105 +
106 +.message {
107 + border-radius: 5px;
108 + padding: 0.5rem;
109 + font-size: 12px;
110 + margin: 0 5px;
111 + flex: 10;
112 +}
113 +
114 +.time {
115 + font-size: 10px;
116 + margin: 0 5px;
117 +}
118 +
119 +.sent {
120 + flex-direction: row-reverse;
121 + float: right;
122 +}
123 +
124 +.sent .message {
125 + background: #9986EE;
126 + color: #fff;
127 +}
128 +
129 +.received .message {
130 + background: #fff;
131 +}
...\ No newline at end of file ...\ No newline at end of file
1 +<!DOCTYPE html>
2 +<html lang="en">
3 +<head>
4 + <meta charset="UTF-8">
5 + <meta http-equiv="X-UA-Compatible" content="IE=edge">
6 + <meta name="viewport" content="width=device-width, initial-scale=1.0">
7 + <title>Document</title>
8 + <link rel="stylesheet" href="css/style.css">
9 +</head>
10 +<body>
11 + <div class="wrapper">
12 + <div class="user-container">
13 + <lable class="nickname" for="nickname">닉네임설정</lable>
14 + <input type="text" id="nickname">
15 + </div>
16 + <div class="display-container">
17 + <ul class="chatting-list">
18 +
19 + </ul>
20 + </div>
21 + <div class="input-container">
22 + <span>
23 + <input type="text" class="chatting-input">
24 + <button class="send-button">전송</button>
25 + </span>
26 + </div>
27 + </div>
28 +
29 + <script src="/socket.io/socket.io.js"></script>
30 + <script src="js/chat.js"></script>
31 +</body>
32 +</html>
...\ No newline at end of file ...\ No newline at end of file
1 +"use strict"
2 +//const socket = io.connect("http://localhost:3000/", {transports:['websocket']});
3 +const socket = io();
4 +
5 +const nickname = document.querySelector("#nickname")
6 +const chatlist = document.querySelector(".chatting-list")
7 +const chatInput = document.querySelector(".chatting-input")
8 +const sendButton = document.querySelector(".send-button")
9 +const displayContainer = document.querySelector(".display-container")
10 +
11 +chatInput.addEventListener("keypress", (event)=> {
12 + if(event.keyCode === 13) {
13 + send()
14 + }
15 +})
16 +
17 +function send() {
18 + const param = {
19 + name: nickname.value,
20 + msg: chatInput.value
21 + }
22 + socket.emit("chatting", param)
23 +}
24 +
25 +sendButton.addEventListener("click", send)
26 +
27 +socket.on("chatting", (data)=>{
28 + console.log(data)
29 + const {name, msg, time} = data;
30 + const item = new LiModel(name, msg, time);
31 + item.makeLi()
32 + displayContainer.scrollTo(0, displayContainer.scrollHeight)
33 +})
34 +
35 +//console.log(socket);
36 +
37 +function LiModel(name, msg, time) {
38 + this.name = name;
39 + this.msg = msg;
40 + this.time = time;
41 +
42 + this.makeLi = ()=>{
43 + const li = document.createElement("li");
44 + li.classList.add(nickname.value === this.name ? "sent":"received")
45 + const dom = `<span class="profile">
46 + <span class="user">${this.name}</span>
47 + <img class="image" src="https://placeimg.com/50/50/any" alt="any">
48 + </span>
49 + <span class="message">${this.msg}</span>
50 + <span class="time">${this.time}</span>`;
51 +
52 + li.innerHTML = dom;
53 + chatlist.appendChild(li)
54 + }
55 +}
...\ No newline at end of file ...\ No newline at end of file
This diff is collapsed. Click to expand it.