-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·135 lines (109 loc) · 3.35 KB
/
server.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
/* jshint indent:2, node:true */
(function() {
"use strict";
require('coffee-script');
require('colors');
var express = require("express");
var http = require("http");
var path = require("path");
var fs = require("fs");
var app = express();
var server = http.createServer(app);
var io = require("socket.io").listen(server);
var PORT = process.env.PORT || 5000;
var routes = require("./routes");
// keyword for tracking
var KEYWORD = ["bad", "evil", "sinister"];
// keep track of players with sockets
var players = {};
// twitter API client: https://github.com/ttezel/twit
var Twit = require('twit');
// create a new twitter client
var T = new Twit({
consumer_key: process.env.consumer_key,
consumer_secret: process.env.consumer_secret,
access_token: process.env.access_token,
access_token_secret: process.env.access_token_secret
});
// filter the twitter public stream by the word 'mango'.
var stream = T.stream('statuses/filter', { track: KEYWORD });
// config the sever
app.configure(function() {
app.set("port", PORT);
app.set("views", "" + __dirname + "/views");
app.set("view engine", "jade");
app.use(express.favicon());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express["static"](path.join(__dirname, "public/")));
return;
});
app.configure("development", function() {
app.use(express.logger("dev"));
app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
app.locals.pretty = true;
return;
});
app.configure("production", function() {
app.use(express.errorHandler());
return;
});
app.get("/", routes.index);
if (!module.parent) {
server.listen(app.get("port"), function() {
console.log(("\n\n==================================================\nExpress server running on: http://localhost:" + (app.get("port")) + "\n==================================================").green);
return;
});
}
io.configure("development", function() {
io.set("log level", 2);
return;
});
io.configure("production", function() {
io.set("transports", ["websocket", "flashsocket", "htmlfile", "xhr-polling", "jsonp-polling"]);
io.set("polling duration", 3);
io.enable("browser client minification");
io.enable("browser client etag");
io.enable("browser client gzip");
io.set("log level", 1);
return;
});
/**
* add player to pool and send a number of total player
*/
io.sockets.on("connection", function(socket) {
players[socket.id] = socket;
/**
* when a new player join
* @return {[type]} [description]
*/
socket.on("player:join", function() {
io.sockets.emit("player:welcome", {
total_player : Object.keys(players).length
});
});
/**
* remove player when disconnect
*/
socket.on("disconnect", function() {
// remove player from pool
delete players[socket.id];
// tell other players about it
io.sockets.emit("player:disconnect", {
total_player : Object.keys(players).length
});
return;
});
return;
});
/**
* send the tweet to all client when available
*/
stream.on('tweet', function (tweet) {
io.sockets.emit("game:tweet", tweet);
});
}).call(this);