/
server-http2.js
142 lines (119 loc) · 4.43 KB
/
server-http2.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
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
136
137
138
139
140
141
142
/**
* A example of Thread-SSE server-side for HTTP/2 over TLS
*
* (c) Copyright 2020-present Richard Li <richard.li@w3plan.net>
* License: MIT
*/
const fs = require('fs');
const path = require('path');
const http2 = require('http2');
// Loads custom validation for custom Thread-SSE data
require('./custom/validation');
// Loads custom access control
const { CustomAccessControl } = require('./custom/access-control');
var tsseObj = null;
try {
// Loads Thread-SSE from npm module
require.resolve("thread-sse");
tsseObj = require('thread-sse');
} catch(e) {
// Loads Thread-SSE from local
tsseObj = require('./index');
}
// destructuring assignment
const {
getConnectionPath,
getTsseServicePath,
getsharedDataPath,
libStyleGraph,
responseClient,
setTsseConnection,
tsseServer,
updateTsseData,
updateGroupTsseData
} = { ...tsseObj };
// Using HTTPS certificate and private key
const HttpsOptions = {
key: fs.readFileSync('./custom/localhost.key'),
cert: fs.readFileSync('./custom/localhost.cert')
};
const server = http2.createSecureServer( HttpsOptions );
// Sets up sample server
server.on('request', function (req, res) {
var requestUrl = req.url;
var tsseServicePath = getTsseServicePath(req);
if (requestUrl === "/js/tsse.js")
{
// the default Thread-SSE client library
libStyleGraph(req, res, "/public/js/tsse.js");
} else if (requestUrl === "/client.html")
{
// the sample file of Thread-SSE client
var filePath = path.join(__dirname, "/public/client.html");
try {
var content = fs.readFileSync(filePath, 'utf8');
responseClient(req, res, content, true);
} catch (er) {
console.log("Server can't read the client file.")
}
} else if (requestUrl === "/js/custom-tsse.js")
{
// the custom Thread-SSE client JavaScript file
libStyleGraph(req, res, "/public/js/custom-tsse.js");
} else if (requestUrl === getConnectionPath())
{
// Handles Thread-SSE connection request
setTsseConnection(req, res);
} else if (requestUrl === tsseServicePath && tsseServicePath !== '')
{
/**
* Applies access control for visitors and logged in users
*
* Controls visitors and logged in users to access tsseServer and updateTsseData
*/
var cac = new CustomAccessControl();
var tsseUser = cac.getTsseUser( req );
// Thread-SSE server for the specific client over HTTP2
tsseServer(tsseServicePath, res, true);
/**
* Using setTimeout as server event to update Thread-SSE data
*/
// Updates the default Thread-SSE data
setTimeout( function(){
updateTsseData(tsseServicePath, tsseUser, "1000", "Sending Thread SSE Data");
}, 4000 );
// Updates the Thread-SSE data with the same id but different contents
setTimeout( function(){
updateTsseData(tsseServicePath, tsseUser, "1000", "Sending repeated data id");
}, 8000 );
// Updates the custom Thread-SSE data
setTimeout( function(){
updateTsseData(tsseServicePath, tsseUser, "4050", "Joanne Whalley", "(647) 823-7580");
}, 12000 );
// Updates the default Thread-SSE data with defined client action
setTimeout( function(){
updateTsseData( tsseServicePath, tsseUser, "8340", "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js;https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js", "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" );
}, 16000 );
// Closes Thread-SSE connection
setTimeout( function(){
updateTsseData(tsseServicePath, tsseUser, "8990");
}, 20000 );
} else if (requestUrl === getsharedDataPath())
{
/**
* Receives shared Thread-SSE data and send it to the members of the group
*
*/
var cac = new CustomAccessControl();
var tsseUser = cac.getTsseUser( req );
var members = cac.getGroupMembersByUser(tsseUser);
updateGroupTsseData(req, res, members);
} else
{
// response Thread-SSE logo
libStyleGraph(req, res, "/public/img/tsse-logo-200x175.jpg");
}
}
);
// Starts sample sever
server.listen(3000, "localhost", () => console.log('The sample app listening at https://localhost:3000'));