-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrapevine_app.js
More file actions
62 lines (54 loc) · 1.95 KB
/
Copy pathgrapevine_app.js
File metadata and controls
62 lines (54 loc) · 1.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
// http server stuff
var express = require('express');
//starting express module
var app = express();
//telling express to root at the public folder
app.use(express.static('public'));
//using http module to create a server out of the express module
var server = require('http').Server(app);
//instantiating socket.io module with server as argument
//any socket made with socket io library will be bound to the server
var io = require('socket.io')(server);
var port = 8090;
//listens on localhost:8090 for event emissions
server.listen(port);
console.log('listening on port ' + 8090);
var grapevine = require('./grapevine');
//define the init function
grapevine.init(function(){
//grapevine.countries ends up being the countries_json data
var countries = grapevine.countries;
//socket is specific to each clients connection
io.on('connection', function (socket) {
var search_query;
console.log("search_query:", search_query);
//when we receive search message, do function(data)
socket.on('search', function (data) {
//data = JSON.parse(data);
var search_query = data.search_query;
console.log("search_query:", search_query);
var countries = data.countries;
var user_language = data.user_language;
console.log("user_language:", user_language);
var result_start = data.result_start;
// console.log("result_start:",result_start);
var grapes = function(i)
{
grapevine.simulate_country(search_query, countries[i], user_language, result_start[countries[i]], function(result){
socket.emit('news', { country_code: countries[i], news: result, more: data.more });
});
};
for (var i = 0; i < countries.length; i++)
{
console.log(i, " ", search_query, " ", countries[i], " ", user_language, " ", result_start[countries[i]])
grapes(i);
}
});
socket.on('error', function (err) {
console.error("Error: " + err);
});
//send an ack message
// note that "emit" does not
socket.emit('ack', countries);
});
});