-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
37 lines (28 loc) · 1.07 KB
/
index.js
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
const express = require('express');
const app = express();
const http = require('http');
const cors = require('cors');
const {Server} = require("socket.io");
app.use(cors());
const server = http.createServer(app);
const io = new Server(server,{
cors:{
origin: "http://localhost:3000",
methods: ["GET", "POST"],
},
});
io.on("connection", (socket) =>{
console.log(`User connnected ${socket.id}`);
socket.on("joinRoom", (data)=>{ // data passes room ID
socket.join(data);
console.log(`user with ID: ${socket.id} joined the room ${data}`)
});
socket.on("send_message", (data) => { // listens for send_message event
// sends message to everyone in room, with this tag, which the clients will await
socket.to(data.room).emit("receive_message", data); // send a message with this event tag
});
socket.on("disconnect", () => {
console.log("User disconnected", socket.id)
});
}) // listening for event connection , prints this on connection
server.listen(3001, () => console.log('server running'));