-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
94 lines (64 loc) · 2.16 KB
/
Copy pathserver.js
File metadata and controls
94 lines (64 loc) · 2.16 KB
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
var http = require('http');
const fs = require('fs');
const https = require('https');
var socketClusterServer = require('socketcluster-server');
var serveStatic = require('serve-static');
var path = require('path');
const bodyparser = require('body-parser');
const app = require('express')();
app.use(serveStatic(path.resolve(__dirname, 'public')));
const PORT = process.env.PORT || 8000;
// ? ====== FOR HTTP CONFIG =========
// var server = http.createServer();
// server.on('request', app);
// server.listen(8000);
// ? ================================
// ? ##################################
// ? ##################################
// ? ##################################
// ? ##################################
// ? ==== FOR HTTPS CONFIG ==================
const server = https.createServer(
{
key: fs.readFileSync('/etc/letsencrypt/live/dreamerslake.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/dreamerslake.com/fullchain.pem'),
},
app
)
.listen(PORT, function () {
console.log(`Server started on port ${PORT}`);
});
// app.enable('trust proxy');
// ? =========================================
// ? ##################################
// ? ##################################
// ? ##################################
app.use(bodyparser.urlencoded({ limit: '50mb', extended: true }));
app.use(bodyparser.json());
// Attach socketcluster-server to our httpServer
var scServer = socketClusterServer.attach(server, {
protocolVersion: 1,
path: '/socketcluster/'
});
scServer.on('connection', function (socket) {
console.log("connected...")
socket.on('message', (data) => {
try {
const yellow = JSON.parse(data)
const message = yellow.data;
scServer.exchange.publish(message.channel, JSON.stringify(message.data));
} catch (error) {
console.log('send error ',error.message)
}
})
});
app.post('/socketserver/publish', (req, res) => {
const { channelName, data } = req.body;
console.log(channelName, data)
try {
scServer.exchange.publish(channelName, JSON.stringify(data));
res.status(200).json({ message: 'Sent' })
} catch (error) {
console.log('route error',error.message)
}
})