-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
142 lines (142 loc) · 5.02 KB
/
index.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
const SocketServer = require('ws').Server;
const express = require('express');
const app = express();
const path = require('path');
const moment = require('moment');
var uuid = require('uuid');
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
const xhr = new XMLHttpRequest();
var dataurl,startdate,enddate;
var connectedUsers = [];
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
var index = require('./routes/index');
app.use('/', index);
app.use('/latest-*', index);
app.use("*",function(req, res, next){
res.status(404).json({"status" : "404"});
next();
});
var server = app.listen(3000, function() {
console.log("websocket-webmap is running at localhost, port 3000");
});
function terminated() {}
function connectionIsAlive() {
this.isAlive = true;
}
const websocketserver = new SocketServer({ server });
websocketserver.on('connection', function connection(websocket) {
var intervalrequest,stringified_geojson;
websocket.id = uuid.v4();
websocket.isAlive = true;
websocket.on('pong', connectionIsAlive);
console.log("a client [id:"+websocket.id+"] is connected...");
websocket.on('message', function incoming(message) {
console.log('A websocket data request is coming: %s \nBegin to process at '+new Date()+'', message);
switch (message) {
case 'request_websocket_data_geofongfz':
intervalrequest = setInterval(function(){
dataurl = "http://geofon.gfz-potsdam.de/eqinfo/list.php?fmt=geojson";
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var data = this.responseText;
if (websocket.readyState !== websocket.OPEN) {
websocket.terminate();
} else {
websocket.send(data);
console.log('Sending data to clientId: '+websocket.id+'...');
}
}
};
xhr.open('GET', dataurl);
xhr.send();
},30*1000);
break;
case 'request_websocket_data_usgs':
intervalrequest = setInterval(function(){
startdate = moment.utc().subtract(24,"hours").format("YYYY-MM-DD");
enddate = moment.utc().format("YYYY-MM-DD");
dataurl = "https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime="+startdate+"&endtime="+enddate+"&minmagnitude=1";
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var data = this.responseText;
if (websocket.readyState !== websocket.OPEN) {
websocket.terminate();
} else {
websocket.send(data);
console.log('Sending data to clientId: '+websocket.id+'...');
}
}
};
xhr.open('GET', dataurl);
xhr.send();
},10*1000);
break;
case 'request_websocket_data_inasafe':
intervalrequest = setInterval(function(){
startdate = moment.utc().subtract(24,"hours").format("YYYY-MM-DD");
enddate = moment.utc().format("YYYY-MM-DD");
dataurl = "http://realtime.inasafe.org/realtime/api/v1/earthquake/?format=json&min_depth=1&max_depth=1000&min_magnitude=1&max_magnitude=10&min_time="+startdate+"&max_time="+enddate+"";
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var data = JSON.parse(this.responseText);
var json_inasafe = data.results;
var geojson = {};
geojson['type'] = 'FeatureCollection';
geojson['metadata'] = [];
var metadata = {
"count": data.count
}
geojson['metadata'].push(metadata);
geojson['features'] = [];
for (var chunk in json_inasafe) {
var featureEntry = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [parseFloat(json_inasafe[chunk].location.coordinates[0]), parseFloat(json_inasafe[chunk].location.coordinates[1])]
},
"properties": {
"mag": json_inasafe[chunk].magnitude,
"place": json_inasafe[chunk].location_description
}
}
geojson['features'].push(featureEntry);
}
stringified_geojson = JSON.stringify(geojson);
if (websocket.readyState !== websocket.OPEN) {
websocket.terminate();
} else {
websocket.send(stringified_geojson);
}
}
};
xhr.open('GET', dataurl);
xhr.send();
},10*1000);
break;
case 'request_terminate_websocket':
websocketserver.clients.forEach(function each(websocket) {
if (websocket.isAlive === true) {
return websocket.terminate();
}
websocket.isAlive = false;
});
break;
default:
console.log('default::undefined');
break;
}
connectedUsers.push(message);
});
});
const interval = setInterval(function ping() {
websocketserver.clients.forEach(function each(websocket) {
if (websocket.isAlive === false) {
return websocket.terminate();
}
websocket.isAlive = false;
websocket.ping(terminated);
});
}, 30*1000);