-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.js
More file actions
135 lines (135 loc) · 4.2 KB
/
backend.js
File metadata and controls
135 lines (135 loc) · 4.2 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// import * as tf from '@tensorflow/tfjs-node';
// const app = require('express')();
// const http = require('http').Server(app);
// const PORT = 3000;
// app.get('/', (req, res) => {
// res.sendFile(__dirname + '/index.html');
// });
// app.get('/frontend.js', (req, res) => {
// res.sendFile(__dirname + '/frontend.js');
// });
// const model = tf.sequential();
// model.add(tf.layers.dense({ units: 1, inputShape: [1] }));
// model.compile({ loss: 'meanSquaredError', optimizer: 'sgd' });
// const xs = tf.tensor2d([1, 2, 3, 4, 5, 6, 7, 8], [8, 1]);
// const ys = tf.tensor2d([1, 3, 5, 7, 9, 11, 13, 15], [8, 1]);
// xs.print();
// ys.print();
// model.fit(xs, ys, { epochs: 10 }).then(() => {
// let prediction = model
// .predict(tf.tensor2d([5], [1, 1]))
// .toString()
// .slice(11);
// if (prediction.charAt(prediction.length - 2) === ',') {
// console.log('Hey');
// prediction = `${prediction.substr(0, prediction.length - 2)}${prediction.substr(
// prediction.length - 1,
// prediction.length
// )}`;
// }
// console.log(JSON.parse(prediction));
// const io = require('socket.io')(http);
// io.on('connection', socket => {
// console.log('A user connected.');
// socket.on('disconnect', () => {
// console.log('A user disconnected');
// });
// socket.on('test', data => {
// console.log(data);
// });
// });
// http.listen(PORT, () => {
// console.log(`Listening on *:${PORT}`);
// });
// });
// VER 1 END
// const PORT = 3000;
// const fs = require('fs');
// const cert = fs.readFileSync('../socketiossl/cert.pem');
// const key = fs.readFileSync('../socketiossl/key.pem');
// const server = require('https').createServer({
// cert: cert,
// key: key,
// });
// // console.log(`Certificate: ${cert}`);
// // console.log(`Key: ${key}`);
// const io = require('socket.io')(server);
// io.on('connection', socket => {
// console.log('A user has connected.');
// socket.on('disconnect', () => console.log('A user has disconnected.'));
// socket.on('test', data => console.log(data));
// });
// server.listen(PORT, () => console.log(`Started listening on localhost:${PORT}`));
// VER 2 END
// const app = require('express')();
// const fs = require('fs');
// const PORT = 8080;
// const SPORT = 8443;
// const cert = fs.readFileSync('../socketiossl/cert.pem', 'utf8');
// const key = fs.readFileSync('../socketiossl/key.pem', 'utf8');
// var credentials = { key: key, cert: cert };
// const server = require('https').createServer({
// cert: cert,
// key: key,
// });
// const httpServer = require('http').createServer(app);
// const httpsServer = require('https').createServer(credentials, app);
// app.get('/', (req, res) => {
// res.sendFile(__dirname + '/index.html');
// });
// app.get('/frontend.js', (req, res) => {
// res.sendFile(__dirname + '/frontend.js');
// });
// const io = require('socket.io')(httpsServer);
// io.on('connection', socket => {
// console.log('A user connected.');
// socket.on('disconnect', () => {
// console.log('A user disconnected');
// });
// socket.on('test', data => {
// console.log(data);
// });
// });
// httpServer.listen(PORT, () => {
// console.log(`Listening HTTP on *:${PORT}`);
// });
// httpsServer.listen(SPORT, () => {
// console.log(`Listening HTTPS on *:${SPORT}`);
// });
// VER 3 END
var WS = require('ws');
var fs = require('fs');
var https = require('https');
var http = require('http');
var PORT = 8080;
var isProduction;
try {
fs.readFileSync('production.json');
isProduction = true;
}
catch (e) {
isProduction = false;
}
var server = isProduction
? http.createServer()
: https.createServer({
cert: fs.readFileSync('../nodessl/cert.pem'),
key: fs.readFileSync('../nodessl/key.pem')
});
var wss = new WS.Server({ server: server });
var useridCounter = 0;
wss.on('connection', function (ws) {
var userid = useridCounter;
useridCounter++;
console.log("User " + userid + " connected.");
ws.on('disconnect', function () {
console.log("User " + userid + " disconnected.");
});
ws.on('message', function (data) {
console.log('Received %s', data);
});
});
server.listen(PORT, function () {
console.log('Started listening on :%s . Is production?: %s', PORT, isProduction);
});
// VER 4 END