-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple-routing-with-websocket.js
More file actions
106 lines (94 loc) · 3.95 KB
/
simple-routing-with-websocket.js
File metadata and controls
106 lines (94 loc) · 3.95 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
const {
WSGILite,
} = require('wsgilite/wsgilite');
const {
Template,
} = require('wsgilite/template');
const {
mimeMap,
defMiddlewareServeFileStatic,
defMiddlewareServeFileStaticWithDirList,
} = require('wsgilite/file');
const {
defFormCsrfCheckRoutes,
defHeaderCsrfCheckRoutes,
getCSRF_token,
generateCSRFFormInput,
} = require('wsgilite/csrf');
// NOTE Test /wss1 websocket under commandline:
// curl --include --no-buffer --header "Connection: Upgrade" --header "Upgrade: websocket" --header "Host: example.com:80" --header "Origin: http://example.com:80" --header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" --header "Sec-WebSocket-Version: 13" http://localhost:3333/wss1
// Settings
const wsgilite = new WSGILite({
secret: 'abcdefg', // The secret key for CSRF
logProcessMessage: true, // Log the multiple processes starting/ending messages.
debug: true, // Debug mode, it will show stacktrace of errors in the responses
isHttps: false, // Is it a https server?
processNum: require('os').cpus().length, // N + 1 processes (cluster: 1 * master + n * fastcgi style http/https serving)
// processNum: 0, // Single process
// createServerOptions: {}, // Additional createServer options for http/https.createServer(options)
onServerCreated: function (httpServer) {
const url = require('url');
const WebSocket = require('ws');
const wss1 = new WebSocket.Server({ noServer: true });
const wss2 = new WebSocket.Server({ noServer: true });
wss1.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('wss1 something');
});
wss2.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('wss2 something');
});
httpServer.on('upgrade', function upgrade(request, socket, head) {
const pathname = url.parse(request.url).pathname;
if (pathname === '/wss1') {
wss1.handleUpgrade(request, socket, head, function done(ws) {
wss1.emit('connection', ws, request);
});
} else if (pathname === '/wss2') {
wss2.handleUpgrade(request, socket, head, function done(ws) {
wss2.emit('connection', ws, request);
});
} else {
socket.destroy();
}
});
}, // Callback on one of servers created(this will be called in Multiple processes)
onMessageMaster: function (worker, msg, handle) {
console.log(`master got message: ${msg.event}`);
}, // Optional: We could monitor and handle communications between cluster master & workers
onMessageWorker: function (msg, handle) {
console.log(`worker got message: ${msg.event}`);
}, // Optional: We could monitor and handle communications between cluster master & workers
workerServeTimesToRestart: 500, // Each child worker will auto-restart after it served 500 requests
});
// Middlewares
wsgilite.addMiddleware(async (request, response, meta)=>{
meta.msg3 = 'I got it3';
});
wsgilite.addMiddleware(function * (request, response, meta) {
return Promise.resolve(0).then(()=>meta.msg2 = 'I got it2');
});
wsgilite.addMiddleware((request, response, meta)=>{
meta.msg = 'I got it';
});
// Routes
// Print the `meta` object
wsgilite.GET('/', (request, response, meta)=>{
return JSON.stringify(meta); // {"_skip404":true,"_url_path":"/","msg3":"I got it3","msg2":"I got it2","msg":"I got it"}
});
// Path parameters(until `/` or `?` or the end)
wsgilite.GET('/user/:id', function *(request, response, meta) {
return yield Promise.resolve(meta); // {"_skip404":true,"_url_path":"/user/theID","msg3":"I got it3","msg2":"I got it2","msg":"I got it","id":"theID"}
});
// Path parameters(until `?` or the end)
wsgilite.GET('/file*relativePath', (request, response, meta)=>{
defMiddlewareServeFileStatic('demo')(request, response, meta);
});
wsgilite.listen(3333, 'localhost', function () {
console.log('Server up');
});