-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
81 lines (72 loc) · 1.83 KB
/
app.js
File metadata and controls
81 lines (72 loc) · 1.83 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
'use strict'
var http = require('http');
var https = require('https');
var path = require('path');
var fs = require('fs');
var express = require('express');
var serveIndex = require('serve-index');
var app = express();
const WebSocket = require('ws');
const WebSocketServer = WebSocket.Server;
class base {
constructor() {
// console.log("BaseClass")
}
}
app.use(serveIndex('./public'));
app.use('/', express.static(path.join(__dirname, './public')));
console.log("http run on :",process.argv.slice(2)[0] || 80)
console.log("https run on :",process.argv.slice(2)[1] || 443)
class ss extends base {
constructor() {
super();
this.DefineEnvironment();
this.CreateHttpServer();
this.CreateHttpServer_SSL();
this.CreateWebSocket();
}
DefineEnvironment(){
this.ssl_options = {
key : fs.readFileSync('./ssl/ca.key'),
cert : fs.readFileSync('./ssl/cert.crt')
}
this.eth = '0.0.0.0'
}
CreateHttpServer(){
//http server
this.http_server = http.createServer(app);
this.http_server.listen(process.argv.slice(2)[0], this.eth);
}
CreateHttpServer_SSL(){
//https server
this.https_server = https.createServer(this.ssl_options, app);
this.https_server.listen(process.argv.slice(2)[1], this.eth);
}
CreateWebSocket(){
var that = this;
this.wss = new WebSocketServer({
server: this.https_server
});
this.wss.on('connection', this.WebSocket_connection);
this.wss.broadcast = function (data) {
that.wss.clients.forEach(function (client) {
if (client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
};
}
WebSocket_connection(ws, req){
ws.on('message', function (message) {
console.log(message);
})
ws.on('close', function (message) {
console.log(message);
})
ws.on('error', function (message) {
console.log('erro');
console.log(message);
})
}
}
let cc= new ss();