-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
38 lines (38 loc) · 1.12 KB
/
server.js
File metadata and controls
38 lines (38 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const express = require('express');
const http = require('http');
const app = express();
const Server = http.createServer(app);
const io = require('socket.io')(Server);
const PORT = process.env.PORT || 5000;
//MIDDLEWARE
app.use(express.static("public"));
app.use(express.urlencoded({extended:true}));
//get
app.get("/",(req,res)=> res.sendFile(__dirname + "/html/index.html"));
app.get("/chat",(req,res)=> res.sendFile(__dirname +"/html/chat.html"));
//post
app.post("/",(req,res)=>{
res.redirect("/chat");
});
//
io.on("connect",socket =>{
var users = {};
socket.on("new-user",name => {
socket.broadcast.emit("new-user",`${name} joined the chat 😊`);
});
socket.on("texts",({text,name}) =>{
users[socket.id] = name;
socket.broadcast.emit("texts",{text,name});
});
socket.on("disconnect",()=>{
if(users[socket.id] != undefined){
socket.broadcast.emit("left",`${users[socket.id]} just left the chat 😞`);
}
delete users[socket.id];
});
});
//
Server.listen(PORT,(err)=>{
if(err) console.log(err)
console.log(`SERVER IS UP ON : ${PORT}`);
});