-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (93 loc) · 2.61 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const express = require('express')
const http = require('http')
const socketIo = require('socket.io')
const mongoose = require('mongoose')
const DataModel = require('./models/Data')
require('dotenv').config()
const { error } = require('console')
const cors = require('cors')
mongoose.connect(
'mongodb+srv://admin:admin123@cluster0.uhlyr.mongodb.net/Code-App?retryWrites=true&w=majority',
{ useNewUrlParser: true }
)
//Port from environment variable or default - 3000
const port = process.env.PORT || 8080
//Setting up express and adding socketIo middleware
const app = express()
app.use(
cors({
origin: 'https://silly-kalam-8f7a8b.netlify.app',
})
)
app.use(express.json())
const server = http.createServer(app)
//const io = socketIo(server)
const io = socketIo(server, {
cors: {
origin: 'https://silly-kalam-8f7a8b.netlify.app',
methods: ['GET', 'POST'],
credentials: true,
},
})
//console.log(io)
//Setting up a socket with the namespace "connection" for new sockets
app.get('/read', async (req, res) => {
const data = await DataModel.find({}, (err, response) => {
if (err) throw error
else {
res.send(response)
}
})
})
app.get('/room/:id', async (req, res) => {
const roomId = req.params.id
await DataModel.findOne({ roomId: roomId }, (error, response) => {
if (error) throw error
else {
res.send(response)
}
})
})
app.post('/room/:id', async (req, res) => {
//console.log('req', req)
//console.log('req.body', req.body)
//console.log(req.params.id)
const roomId = req.params.id
const text = req.body.text
const langauge = req.body.langauge
const theme = req.body.theme
const input = req.body.input
const output = req.body.output
await DataModel.updateOne(
{ roomId: roomId },
[
{
$set: {
roomId: roomId,
text: text,
langauge: langauge,
theme: theme,
input: input,
output: output,
},
},
],
{ upsert: true }
)
})
io.on('connection', (socket) => {
console.log('New client connected')
socket.on('join', (room) => {
console.log('room', room)
socket.join(room)
})
//Here we listen on a new namespace called "incoming data"
socket.on('data', (data) => {
//Here we broadcast it out to all other sockets EXCLUDING the socket which sent us the data
console.log('data', data)
socket.to(data.room).emit('data', { data: data })
})
//A special namespace "disconnect" for when a client disconnects
socket.on('disconnect', () => console.log('Client disconnected'))
})
server.listen(port, () => console.log(`Listening on port ${port}`))