-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
86 lines (72 loc) · 2.41 KB
/
main.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
const fs = require('fs')
const cors = require('cors')
const express = require('express')
// var privateKey = fs.readFileSync('192.168.43.119.key');
// var certificate = fs.readFileSync('192.168.43.119.cert');
const WebSocket = require('ws');
const WebSocketServer = WebSocket.Server;
//{key:privateKey,cert:certificate},
var app = require('express')(),
// server = require('https').createServer({key:privateKey,cert:certificate},app)
server = require('https').createServer(app)
const wss = new WebSocketServer({ server: server });
require('dotenv').config()
app.use(cors())
app.use("/auth", require("./jwtAuth"))
app.use(express.json());
app.use('/uploads', express.static('./public/images'));
wss.on('connection', function(ws) {
ws.room = []
// ws.send(JSON.stringify({msg:"user joined"}));
// console.log('User connected');
ws.on('message', function(message) {
// Broadcast any received message to all clients
var message = JSON.parse(message);
if (message.message) {
message['time'] = new Date()
console.log(message)
}
var roomName = message.roomName
if (roomName) {
if (!ws.room.includes(roomName)) {
ws.room.push(roomName)
}
}
console.log(ws.room)
if (message.roomName) {
if (message.close) {
ws.close()
}
wss.broadcast(JSON.stringify(message));
}
// if(messag.msg){console.log('message: ',messag.msg)}
// console.log(ws.room)
console.log('received: %s', message);
// wss.broadcast(message);
});
ws.on('error', () => ws.terminate());
});
wss.broadcast = function(data) {
this.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
if (client.room.indexOf(JSON.parse(data).roomName) > -1) {
client.send(data)
}
}
});
};
setInterval(() => {
wss.clients.forEach((client) => {
if(client.readyState === WebSocket.OPEN)
{client.send(JSON.stringify(new Date().toTimeString()));}
});
}, 1000);
app.start = app.listen = function() {
console.log('server running on localhost || 192.168.43.119: '+process.env.PORT)
return server.listen.apply(server, arguments)
}
app.start(process.env.PORT || 80)
app.get('/',(req,res)=>{
res.send('backend working 👍')
})
// app.start(5000)