akarshanxyz 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/a10/Pagination+FilteringwithReduxCache +422 -0
- package/a11/q1.txt +266 -0
- package/a12/(NestedObjectPractice)graphql.txt +85 -0
- package/a12/CustomerSupportSystem(graphql).txt +313 -0
- package/a12/GraphQLReactFrontend.txt +363 -0
- package/a12/StudentInformationSystem(graphql).txt +69 -0
- package/a13/BookStoreManagementSystem.txt +236 -0
- package/a7/Login&PaymentForm(Using Yup) +132 -0
- package/a7/SellerRegistrationForm(zodschemavalidation) +199 -0
- package/a7/UserRegistrationForm(zodschemavalidation) +32 -0
- package/a8/MiniE-commerceApp(using redux) +49 -0
- package/a9/PerformanceOptimization +141 -0
- package/package.json +2 -2
- /package/a1/{array → array.txt} +0 -0
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import mongoose from "mongoose";
|
|
3
|
+
import cors from "cors";
|
|
4
|
+
|
|
5
|
+
import React, { useEffect, useState } from "react";
|
|
6
|
+
import {
|
|
7
|
+
configureStore,
|
|
8
|
+
createAsyncThunk,
|
|
9
|
+
createSlice,
|
|
10
|
+
} from "@reduxjs/toolkit";
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
Provider,
|
|
14
|
+
useDispatch,
|
|
15
|
+
useSelector,
|
|
16
|
+
} from "react-redux";
|
|
17
|
+
|
|
18
|
+
/* ================= BACKEND ================= */
|
|
19
|
+
|
|
20
|
+
const app = express();
|
|
21
|
+
|
|
22
|
+
app.use(cors());
|
|
23
|
+
app.use(express.json());
|
|
24
|
+
|
|
25
|
+
mongoose.connect("mongodb://127.0.0.1:27017/ecommerce");
|
|
26
|
+
|
|
27
|
+
const productSchema = new mongoose.Schema({
|
|
28
|
+
title: String,
|
|
29
|
+
price: Number,
|
|
30
|
+
category: String,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const Product = mongoose.model(
|
|
34
|
+
"Product",
|
|
35
|
+
productSchema
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
const seedProducts = async () => {
|
|
39
|
+
const count = await Product.countDocuments();
|
|
40
|
+
|
|
41
|
+
if (count === 0) {
|
|
42
|
+
await Product.insertMany([
|
|
43
|
+
{
|
|
44
|
+
title: "Laptop",
|
|
45
|
+
price: 50000,
|
|
46
|
+
category: "electronics",
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
title: "Phone",
|
|
50
|
+
price: 20000,
|
|
51
|
+
category: "electronics",
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
title: "TV",
|
|
55
|
+
price: 40000,
|
|
56
|
+
category: "electronics",
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
title: "Speaker",
|
|
60
|
+
price: 5000,
|
|
61
|
+
category: "electronics",
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
title: "Camera",
|
|
65
|
+
price: 35000,
|
|
66
|
+
category: "electronics",
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
{
|
|
70
|
+
title: "Shirt",
|
|
71
|
+
price: 1500,
|
|
72
|
+
category: "fashion",
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
title: "Jeans",
|
|
76
|
+
price: 2500,
|
|
77
|
+
category: "fashion",
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
title: "Shoes",
|
|
81
|
+
price: 3000,
|
|
82
|
+
category: "fashion",
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
title: "Jacket",
|
|
86
|
+
price: 4500,
|
|
87
|
+
category: "fashion",
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
title: "Watch",
|
|
91
|
+
price: 5000,
|
|
92
|
+
category: "fashion",
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
{
|
|
96
|
+
title: "Rice",
|
|
97
|
+
price: 1200,
|
|
98
|
+
category: "grocery",
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
title: "Milk",
|
|
102
|
+
price: 60,
|
|
103
|
+
category: "grocery",
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
title: "Bread",
|
|
107
|
+
price: 40,
|
|
108
|
+
category: "grocery",
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
title: "Eggs",
|
|
112
|
+
price: 120,
|
|
113
|
+
category: "grocery",
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
title: "Sugar",
|
|
117
|
+
price: 50,
|
|
118
|
+
category: "grocery",
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
{
|
|
122
|
+
title: "Keyboard",
|
|
123
|
+
price: 1800,
|
|
124
|
+
category: "electronics",
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
title: "Mouse",
|
|
128
|
+
price: 700,
|
|
129
|
+
category: "electronics",
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
title: "Sneakers",
|
|
133
|
+
price: 3500,
|
|
134
|
+
category: "fashion",
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
title: "Oil",
|
|
138
|
+
price: 180,
|
|
139
|
+
category: "grocery",
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
title: "T-shirt",
|
|
143
|
+
price: 800,
|
|
144
|
+
category: "fashion",
|
|
145
|
+
},
|
|
146
|
+
]);
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
seedProducts();
|
|
151
|
+
|
|
152
|
+
app.get("/products", async (req, res) => {
|
|
153
|
+
const page = Number(req.query.page) || 1;
|
|
154
|
+
const limit = Number(req.query.limit) || 5;
|
|
155
|
+
|
|
156
|
+
const totalProducts =
|
|
157
|
+
await Product.countDocuments();
|
|
158
|
+
|
|
159
|
+
const totalPages = Math.ceil(
|
|
160
|
+
totalProducts / limit
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
const products = await Product.find()
|
|
164
|
+
.skip((page - 1) * limit)
|
|
165
|
+
.limit(limit);
|
|
166
|
+
|
|
167
|
+
res.json({
|
|
168
|
+
products,
|
|
169
|
+
currentPage: page,
|
|
170
|
+
totalPages,
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
app.get(
|
|
175
|
+
"/products/category/:category",
|
|
176
|
+
async (req, res) => {
|
|
177
|
+
const products = await Product.find({
|
|
178
|
+
category: req.params.category,
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
res.json({ products });
|
|
182
|
+
}
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
app.listen(5000, () => {
|
|
186
|
+
console.log("Server running on port 5000");
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
/* ================= FRONTEND ================= */
|
|
190
|
+
|
|
191
|
+
type ProductType = {
|
|
192
|
+
_id: string;
|
|
193
|
+
title: string;
|
|
194
|
+
price: number;
|
|
195
|
+
category: string;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
type ProductState = {
|
|
199
|
+
pages: {
|
|
200
|
+
[key: number]: ProductType[];
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
currentPage: number;
|
|
204
|
+
totalPages: number;
|
|
205
|
+
filteredProducts: ProductType[];
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
const fetchProducts = createAsyncThunk(
|
|
209
|
+
"products/fetchProducts",
|
|
210
|
+
async (page: number) => {
|
|
211
|
+
const response = await fetch(
|
|
212
|
+
`http://localhost:5000/products?page=${page}&limit=5`
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
return response.json();
|
|
216
|
+
}
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
const fetchCategoryProducts =
|
|
220
|
+
createAsyncThunk(
|
|
221
|
+
"products/fetchCategoryProducts",
|
|
222
|
+
async (category: string) => {
|
|
223
|
+
const response = await fetch(
|
|
224
|
+
`http://localhost:5000/products/category/${category}`
|
|
225
|
+
);
|
|
226
|
+
|
|
227
|
+
return response.json();
|
|
228
|
+
}
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
const initialState: ProductState = {
|
|
232
|
+
pages: {},
|
|
233
|
+
currentPage: 1,
|
|
234
|
+
totalPages: 1,
|
|
235
|
+
filteredProducts: [],
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
const productSlice = createSlice({
|
|
239
|
+
name: "products",
|
|
240
|
+
|
|
241
|
+
initialState,
|
|
242
|
+
|
|
243
|
+
reducers: {
|
|
244
|
+
setCurrentPage: (state, action) => {
|
|
245
|
+
state.currentPage = action.payload;
|
|
246
|
+
},
|
|
247
|
+
},
|
|
248
|
+
|
|
249
|
+
extraReducers: (builder) => {
|
|
250
|
+
builder.addCase(
|
|
251
|
+
fetchProducts.fulfilled,
|
|
252
|
+
(state, action) => {
|
|
253
|
+
state.pages[action.payload.currentPage] =
|
|
254
|
+
action.payload.products;
|
|
255
|
+
|
|
256
|
+
state.currentPage =
|
|
257
|
+
action.payload.currentPage;
|
|
258
|
+
|
|
259
|
+
state.totalPages =
|
|
260
|
+
action.payload.totalPages;
|
|
261
|
+
}
|
|
262
|
+
);
|
|
263
|
+
|
|
264
|
+
builder.addCase(
|
|
265
|
+
fetchCategoryProducts.fulfilled,
|
|
266
|
+
(state, action) => {
|
|
267
|
+
state.filteredProducts =
|
|
268
|
+
action.payload.products;
|
|
269
|
+
}
|
|
270
|
+
);
|
|
271
|
+
},
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
const { setCurrentPage } =
|
|
275
|
+
productSlice.actions;
|
|
276
|
+
|
|
277
|
+
const store = configureStore({
|
|
278
|
+
reducer: {
|
|
279
|
+
products: productSlice.reducer,
|
|
280
|
+
},
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
type RootState = ReturnType<
|
|
284
|
+
typeof store.getState
|
|
285
|
+
>;
|
|
286
|
+
|
|
287
|
+
type AppDispatch = typeof store.dispatch;
|
|
288
|
+
|
|
289
|
+
const ProductList = () => {
|
|
290
|
+
const dispatch = useDispatch<AppDispatch>();
|
|
291
|
+
|
|
292
|
+
const {
|
|
293
|
+
pages,
|
|
294
|
+
currentPage,
|
|
295
|
+
totalPages,
|
|
296
|
+
filteredProducts,
|
|
297
|
+
} = useSelector(
|
|
298
|
+
(state: RootState) => state.products
|
|
299
|
+
);
|
|
300
|
+
|
|
301
|
+
const [category, setCategory] =
|
|
302
|
+
useState("");
|
|
303
|
+
|
|
304
|
+
useEffect(() => {
|
|
305
|
+
if (!pages[currentPage]) {
|
|
306
|
+
dispatch(fetchProducts(currentPage));
|
|
307
|
+
}
|
|
308
|
+
}, [currentPage]);
|
|
309
|
+
|
|
310
|
+
const handleNext = () => {
|
|
311
|
+
if (currentPage < totalPages) {
|
|
312
|
+
dispatch(
|
|
313
|
+
setCurrentPage(currentPage + 1)
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
const handlePrevious = () => {
|
|
319
|
+
if (currentPage > 1) {
|
|
320
|
+
dispatch(
|
|
321
|
+
setCurrentPage(currentPage - 1)
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
const handleCategory = (
|
|
327
|
+
e: React.ChangeEvent<HTMLSelectElement>
|
|
328
|
+
) => {
|
|
329
|
+
const value = e.target.value;
|
|
330
|
+
|
|
331
|
+
setCategory(value);
|
|
332
|
+
|
|
333
|
+
if (value) {
|
|
334
|
+
dispatch(fetchCategoryProducts(value));
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
const products = category
|
|
339
|
+
? filteredProducts
|
|
340
|
+
: pages[currentPage] || [];
|
|
341
|
+
|
|
342
|
+
return (
|
|
343
|
+
<div style={{ padding: "20px" }}>
|
|
344
|
+
<h1>Product Listing</h1>
|
|
345
|
+
|
|
346
|
+
<select
|
|
347
|
+
value={category}
|
|
348
|
+
onChange={handleCategory}
|
|
349
|
+
>
|
|
350
|
+
<option value="">All</option>
|
|
351
|
+
|
|
352
|
+
<option value="electronics">
|
|
353
|
+
Electronics
|
|
354
|
+
</option>
|
|
355
|
+
|
|
356
|
+
<option value="fashion">
|
|
357
|
+
Fashion
|
|
358
|
+
</option>
|
|
359
|
+
|
|
360
|
+
<option value="grocery">
|
|
361
|
+
Grocery
|
|
362
|
+
</option>
|
|
363
|
+
</select>
|
|
364
|
+
|
|
365
|
+
<div>
|
|
366
|
+
{products.map((product) => (
|
|
367
|
+
<div
|
|
368
|
+
key={product._id}
|
|
369
|
+
style={{
|
|
370
|
+
border: "1px solid gray",
|
|
371
|
+
margin: "10px 0",
|
|
372
|
+
padding: "10px",
|
|
373
|
+
}}
|
|
374
|
+
>
|
|
375
|
+
<h3>{product.title}</h3>
|
|
376
|
+
|
|
377
|
+
<p>₹{product.price}</p>
|
|
378
|
+
|
|
379
|
+
<p>{product.category}</p>
|
|
380
|
+
</div>
|
|
381
|
+
))}
|
|
382
|
+
</div>
|
|
383
|
+
|
|
384
|
+
{!category && (
|
|
385
|
+
<div>
|
|
386
|
+
<button
|
|
387
|
+
onClick={handlePrevious}
|
|
388
|
+
disabled={currentPage === 1}
|
|
389
|
+
>
|
|
390
|
+
Previous
|
|
391
|
+
</button>
|
|
392
|
+
|
|
393
|
+
<span
|
|
394
|
+
style={{ margin: "0 10px" }}
|
|
395
|
+
>
|
|
396
|
+
Page {currentPage} of{" "}
|
|
397
|
+
{totalPages}
|
|
398
|
+
</span>
|
|
399
|
+
|
|
400
|
+
<button
|
|
401
|
+
onClick={handleNext}
|
|
402
|
+
disabled={
|
|
403
|
+
currentPage === totalPages
|
|
404
|
+
}
|
|
405
|
+
>
|
|
406
|
+
Next
|
|
407
|
+
</button>
|
|
408
|
+
</div>
|
|
409
|
+
)}
|
|
410
|
+
</div>
|
|
411
|
+
);
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
const App = () => {
|
|
415
|
+
return (
|
|
416
|
+
<Provider store={store}>
|
|
417
|
+
<ProductList />
|
|
418
|
+
</Provider>
|
|
419
|
+
);
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
export default App;
|
package/a11/q1.txt
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
const express = require("express");
|
|
2
|
+
const http = require("http");
|
|
3
|
+
const { Server } = require("socket.io");
|
|
4
|
+
|
|
5
|
+
const app = express();
|
|
6
|
+
const server = http.createServer(app);
|
|
7
|
+
const io = new Server(server);
|
|
8
|
+
|
|
9
|
+
let connectedUsers = 0;
|
|
10
|
+
|
|
11
|
+
app.get("/", (req, res) => {
|
|
12
|
+
res.send(`
|
|
13
|
+
<!DOCTYPE html>
|
|
14
|
+
<html>
|
|
15
|
+
<head>
|
|
16
|
+
<title>Real-Time Chat App</title>
|
|
17
|
+
<script src="/socket.io/socket.io.js"></script>
|
|
18
|
+
<style>
|
|
19
|
+
body{
|
|
20
|
+
font-family:Arial;
|
|
21
|
+
margin:20px;
|
|
22
|
+
}
|
|
23
|
+
nav button{
|
|
24
|
+
margin-right:10px;
|
|
25
|
+
padding:8px 15px;
|
|
26
|
+
}
|
|
27
|
+
.page{
|
|
28
|
+
display:none;
|
|
29
|
+
margin-top:20px;
|
|
30
|
+
}
|
|
31
|
+
.active{
|
|
32
|
+
display:block;
|
|
33
|
+
}
|
|
34
|
+
input{
|
|
35
|
+
margin:5px;
|
|
36
|
+
padding:5px;
|
|
37
|
+
}
|
|
38
|
+
#messages,#roomMessages{
|
|
39
|
+
border:1px solid black;
|
|
40
|
+
height:200px;
|
|
41
|
+
overflow-y:auto;
|
|
42
|
+
padding:10px;
|
|
43
|
+
margin-top:10px;
|
|
44
|
+
}
|
|
45
|
+
</style>
|
|
46
|
+
</head>
|
|
47
|
+
<body>
|
|
48
|
+
|
|
49
|
+
<h1>Real-Time Chat Application</h1>
|
|
50
|
+
|
|
51
|
+
<nav>
|
|
52
|
+
<button onclick="showPage('home')">Home</button>
|
|
53
|
+
<button onclick="showPage('private')">Private Chat</button>
|
|
54
|
+
<button onclick="showPage('room')">Room Chat</button>
|
|
55
|
+
</nav>
|
|
56
|
+
|
|
57
|
+
<!-- HOME -->
|
|
58
|
+
<div id="home" class="page active">
|
|
59
|
+
<h2>Home</h2>
|
|
60
|
+
|
|
61
|
+
<p><b>Your Socket ID:</b></p>
|
|
62
|
+
<p id="socketId"></p>
|
|
63
|
+
|
|
64
|
+
<p><b>Connected Users:</b></p>
|
|
65
|
+
<p id="userCount">0</p>
|
|
66
|
+
|
|
67
|
+
<h3>Notifications</h3>
|
|
68
|
+
<div id="notifications"></div>
|
|
69
|
+
</div>
|
|
70
|
+
|
|
71
|
+
<!-- PRIVATE CHAT -->
|
|
72
|
+
<div id="private" class="page">
|
|
73
|
+
<h2>Private Chat</h2>
|
|
74
|
+
|
|
75
|
+
<input id="receiverId" placeholder="Receiver Socket ID">
|
|
76
|
+
|
|
77
|
+
<input id="privateMessage" placeholder="Message">
|
|
78
|
+
|
|
79
|
+
<button onclick="sendPrivateMessage()">
|
|
80
|
+
Send
|
|
81
|
+
</button>
|
|
82
|
+
|
|
83
|
+
<div id="messages"></div>
|
|
84
|
+
</div>
|
|
85
|
+
|
|
86
|
+
<!-- ROOM CHAT -->
|
|
87
|
+
<div id="room" class="page">
|
|
88
|
+
<h2>Room Chat</h2>
|
|
89
|
+
|
|
90
|
+
<input id="roomName" placeholder="Room Name">
|
|
91
|
+
|
|
92
|
+
<button onclick="joinRoom()">
|
|
93
|
+
Join Room
|
|
94
|
+
</button>
|
|
95
|
+
|
|
96
|
+
<br>
|
|
97
|
+
|
|
98
|
+
<input id="roomMessage" placeholder="Room Message">
|
|
99
|
+
|
|
100
|
+
<button onclick="sendRoomMessage()">
|
|
101
|
+
Send
|
|
102
|
+
</button>
|
|
103
|
+
|
|
104
|
+
<div id="roomMessages"></div>
|
|
105
|
+
</div>
|
|
106
|
+
|
|
107
|
+
<script>
|
|
108
|
+
|
|
109
|
+
const socket = io();
|
|
110
|
+
|
|
111
|
+
let currentRoom = "";
|
|
112
|
+
|
|
113
|
+
function showPage(page){
|
|
114
|
+
document.querySelectorAll(".page")
|
|
115
|
+
.forEach(p => p.classList.remove("active"));
|
|
116
|
+
|
|
117
|
+
document
|
|
118
|
+
.getElementById(page)
|
|
119
|
+
.classList.add("active");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
socket.on("connect",()=>{
|
|
123
|
+
document.getElementById("socketId").innerText = socket.id;
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
socket.on("userCount",(count)=>{
|
|
127
|
+
document.getElementById("userCount").innerText = count;
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
socket.on("welcome",(msg)=>{
|
|
131
|
+
const div = document.createElement("div");
|
|
132
|
+
div.innerText = msg;
|
|
133
|
+
document
|
|
134
|
+
.getElementById("notifications")
|
|
135
|
+
.appendChild(div);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
function sendPrivateMessage(){
|
|
139
|
+
|
|
140
|
+
const receiver =
|
|
141
|
+
document.getElementById("receiverId").value;
|
|
142
|
+
|
|
143
|
+
const message =
|
|
144
|
+
document.getElementById("privateMessage").value;
|
|
145
|
+
|
|
146
|
+
socket.emit("privateMessage",{
|
|
147
|
+
receiver,
|
|
148
|
+
message
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
socket.on("privateMessage",(data)=>{
|
|
153
|
+
|
|
154
|
+
const div = document.createElement("div");
|
|
155
|
+
|
|
156
|
+
div.innerText =
|
|
157
|
+
data.sender + ": " + data.message;
|
|
158
|
+
|
|
159
|
+
document
|
|
160
|
+
.getElementById("messages")
|
|
161
|
+
.appendChild(div);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
function joinRoom(){
|
|
165
|
+
|
|
166
|
+
currentRoom =
|
|
167
|
+
document.getElementById("roomName").value;
|
|
168
|
+
|
|
169
|
+
socket.emit("joinRoom", currentRoom);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function sendRoomMessage(){
|
|
173
|
+
|
|
174
|
+
const msg =
|
|
175
|
+
document.getElementById("roomMessage").value;
|
|
176
|
+
|
|
177
|
+
socket.emit("roomMessage",{
|
|
178
|
+
room: currentRoom,
|
|
179
|
+
message: msg
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
socket.on("roomMessage",(data)=>{
|
|
184
|
+
|
|
185
|
+
const div = document.createElement("div");
|
|
186
|
+
|
|
187
|
+
div.innerText =
|
|
188
|
+
data.sender + ": " + data.message;
|
|
189
|
+
|
|
190
|
+
document
|
|
191
|
+
.getElementById("roomMessages")
|
|
192
|
+
.appendChild(div);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
socket.on("roomNotification",(msg)=>{
|
|
196
|
+
|
|
197
|
+
const div = document.createElement("div");
|
|
198
|
+
|
|
199
|
+
div.innerText = msg;
|
|
200
|
+
|
|
201
|
+
document
|
|
202
|
+
.getElementById("roomMessages")
|
|
203
|
+
.appendChild(div);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
</script>
|
|
207
|
+
|
|
208
|
+
</body>
|
|
209
|
+
</html>
|
|
210
|
+
`);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
io.on("connection", (socket) => {
|
|
214
|
+
|
|
215
|
+
connectedUsers++;
|
|
216
|
+
|
|
217
|
+
io.emit("userCount", connectedUsers);
|
|
218
|
+
|
|
219
|
+
io.emit(
|
|
220
|
+
"welcome",
|
|
221
|
+
"User joined: " + socket.id
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
socket.on("privateMessage", (data) => {
|
|
225
|
+
|
|
226
|
+
io.to(data.receiver).emit(
|
|
227
|
+
"privateMessage",
|
|
228
|
+
{
|
|
229
|
+
sender: socket.id,
|
|
230
|
+
message: data.message
|
|
231
|
+
}
|
|
232
|
+
);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
socket.on("joinRoom", (room) => {
|
|
236
|
+
|
|
237
|
+
socket.join(room);
|
|
238
|
+
|
|
239
|
+
socket.to(room).emit(
|
|
240
|
+
"roomNotification",
|
|
241
|
+
socket.id + " joined room " + room
|
|
242
|
+
);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
socket.on("roomMessage", (data) => {
|
|
246
|
+
|
|
247
|
+
io.to(data.room).emit(
|
|
248
|
+
"roomMessage",
|
|
249
|
+
{
|
|
250
|
+
sender: socket.id,
|
|
251
|
+
message: data.message
|
|
252
|
+
}
|
|
253
|
+
);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
socket.on("disconnect", () => {
|
|
257
|
+
|
|
258
|
+
connectedUsers--;
|
|
259
|
+
|
|
260
|
+
io.emit("userCount", connectedUsers);
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
server.listen(3000, () => {
|
|
265
|
+
console.log("Server running on port 3000");
|
|
266
|
+
});
|