-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
69 lines (59 loc) · 2.36 KB
/
app.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
var express = require('express');
var bodyParser = require('body-parser');
var mysql = require('mysql');
var http = require('http');
var socketIO = require('socket.io');
var fs = require('fs');
var path = require('path');
var polyline = require('@mapbox/polyline');
const app = express();
config = require('config');
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json());
app.get('/', function(req,res){
res.sendFile(__dirname + '/index.html');
});
connection = mysql.createConnection(config.get('database_settings'));
connection.connect(function(err) { // The server is either down
if(err) { // or restarting (takes a while sometimes).
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
}else{
console.log('database connected at...', config.get('database_settings.mysqlPORT'));
} // to avoid a hot loop, and to allow our node script to
}); // process asynchronous requests in the meantime.
var server = app.listen(config.get('PORT'), function () {
console.log(`Server running at ${config.get('PORT')} `);
});
var io = socketIO(server);
var chunks = [];
var delay = 0;
var latitude, longitude, ride_id;
io.on('connection', function(socket){
socket.on('message-input', function( data ) {
latitude = data.latitude;
longitude = data.longitude;
ride_id = data.ride_id;
var sql = ` INSERT INTO data SET ? `
connection.query(sql, [data], function(err, result){
if(err){
console.log(err);
}
})
});
socket.on('message-request', function( data ) {
let polylineData=[];
var sql = ` SELECT * FROM data WHERE ride_id = ? `
connection.query(sql, [data], function(err, result){
if(err){
console.log("error", err);
}else{
result.map(function(temp){
polylineData.push([parseFloat(temp.latitude),parseFloat(temp.longitude)]);
});
let polylineString=polyline.encode(polylineData);
socket.emit('message-output', polylineString);
}
})
});
});