-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
45 lines (29 loc) · 745 Bytes
/
Copy pathserver.js
File metadata and controls
45 lines (29 loc) · 745 Bytes
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
39
40
41
const express = require("express");
const Socket = require("socket.io");
const app = express();
const server = require("http").createServer(app);
const io = Socket(server,{
cors: {
origin:"*",
methods:["GET","POST"]
}
})
let PORT = 5000;
server.listen(PORT,() => {
console.log("listening to port: ",PORT)
})
const users = [];
io.on("connection",(socket)=>{
console.log("Connected to ",socket.id)
socket.on("adduser",(username)=>{
socket.user = username;
users.push(username);
io.sockets.emit("users",users)
})
socket.on("message",(message)=>{
io.sockets.emit("message_client",{
message,
user:socket.user
})
})
})