-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (32 loc) · 867 Bytes
/
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
38
39
40
41
import { Server } from "socket.io";
import express from "express"
import cors from "cors"
import http from "http"
import dotenv from 'dotenv'
import { createServer } from "http";
const app = express()
app.use(cors())
const server = createServer(app)
dotenv.config()
const io = new Server(server,{
cors: {
origin: ["http://localhost:3000","https://charming-sprinkles-819e65.netlify.app"],
methods: ["GET", "POST"],
},
})
var port = process.env.PORT
io.of("/").on("connection",(socket)=>{
socket.on("join_room",(data)=>{
socket.join(data)
console.log(data)
})
socket.on("send_msg",(message)=>{
socket.to(message.room).emit("receive",message);
})
socket.on("disconnect",()=>{
console.log("user disconste")
})
})
server.listen(port,()=>{
console.log("server is ready"+port)
});