-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshutdown.js
112 lines (92 loc) · 3.25 KB
/
shutdown.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
//-- ***** BEGIN LICENCE BLOCK *****
// -
// - The Initial Developer of the Original Code is "CoralTree Systems Ltd: http://www.coraltree.co.uk".
// - Portions created by "CoralTree Systems Ltd" are Copyright (c) 2005-2016 CoralTree Systems Ltd.
// - All Rights Reserved.
// -
// - ***** END LICENCE BLOCK *****
//------------------------------------------------------------
//-- File: shutdown.js
//-- Description: Shutdown web socket server
//-- Author: Kevin Turner
//-- Date: Sept 2017
//------------------------------------------------------------
//
// Modification log
// ================
// Inits Date Modification
// ===== ==== ============
//
// ===================================================================
var socketIOClient = require('socket.io-client');
var sailsIOClient = require('sails.io.js');
// Ensure we're in the project directory, so relative paths work as expected
// no matter where we actually run from.
process.chdir(__dirname);
// Obtain the Url from the command line arguments
// An example might be node shutdown.js --port 8686 --host localhost --protocol http
var cfg=require("./config/local");
var protocol="http";
var host="localhost";
var port=cfg.port;
if (process.argv.length>=2) {
// Find protocol
var i=process.argv.indexOf("--protocol");
if (i>=0) {
protocol=process.argv[i+1]
}
// Find host
var i=process.argv.indexOf("--host");
if (i>=0) {
host=process.argv[i+1]
}
// Find port
var i=process.argv.indexOf("--port");
if (i>=0) {
port=process.argv[i+1]
}
}
// Instantiate the socket client (`io`)
// (for now, you must explicitly pass in the socket.io client when using this library from Node.js)
var io = sailsIOClient(socketIOClient);
io.sails.autoConnect=false;
io.sails.transports=['websocket','polling'];
// Set the url
var url=protocol+"://"+host+":"+port;
var opts={};
opts.useCORSRouteToGetCookie=false;
opts.forceNew=true;
if (protocol=="https") {
opts.secure=true;
opts.rejectUnauthorized=false;
}
console.log("Connecting to "+url+"...");
var connector=io.sails.connect(url,opts);
connector.on('connect', function(){
console.log("Connected successfully to "+url);
// Send the request to shutdown
// Post the shutdown
connector.get('/shutdown', function serverResponded (body, JWR) {
if (body.ok) {
console.log("Shutdown request accepted");
}
else {
console.log("Shutdown request rejected");
}
// body === JWR.body
/*
console.log('Sails responded with: ', body);
console.log('with headers: ', JWR.headers);
console.log('and with status code: ', JWR.statusCode);
*/
// When you are finished with `io.socket`, or any other sockets you connect manually,
// you should make sure and disconnect them, e.g.:
connector.disconnect();
});
});
connector.on('disconnect', function(){
console.log("Disconnected")
process.exit(0);
});
// Give up after 10 seconds regardless
setTimeout(function(){process.exit(1); },10000)