-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
64 lines (64 loc) · 2.43 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
var express = require('express');
var bodyParser = require('body-parser');
var weather_1 = require('./weather');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
app.set('port', process.env.PORT || 3000);
app.use(bodyParser.json());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use('/', express.static('public'));
server.listen(app.get('port'), function () {
var host = server.address().address;
var port = server.address().port;
console.log('Listening at port %s', app.get('port'));
});
io.on('connection', function (socket) {
console.log('Connection Successful with id: ' + socket.id);
var weather = new weather_1.Weather();
weather.weatherDataSubject.subscribe(function (result) {
var cities = Object.keys(result), mapData = [];
cities.forEach(function (city) {
mapData.push({ city: city, latitude: result[city].latitude, longitude: result[city].longitude, weather: result[city].currently });
});
var weatherData = mapData.map(function (d) {
return {
city: d.city,
position: {
lat: d.latitude,
lon: d.longitude
},
weather: {
temperature: d.weather.temperature,
icon: 'wi-forecast-io-' + d.weather.icon,
summary: d.weather.summary
}
};
});
socket.emit('weatherData', weatherData);
console.log('weatherDataEmitted to ' + socket.id);
}, function (error) {
console.error(error);
});
weather.weatherDetailSubject.subscribe(function (result) {
result.icon = 'wi-forecast-io-' + result.icon;
socket.emit('cityWeatherDetails', result);
console.log('weatherDetailsEmitted to ' + socket.id);
}, function (error) {
console.error(error);
});
weather.weatherForcastSubject.subscribe(function (result) {
socket.emit('cityForcastDetails', result);
console.log('weatherForcastEmitted to ' + socket.id);
}, function (error) {
console.error(error);
});
socket.on('getCityDetails', function (city) {
weather.getWeatherDetails(city);
});
});
//# sourceMappingURL=app.js.map