-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
231 lines (188 loc) · 5.84 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* Module dependencies.
*/
var express = require('express')
, socketio = require('socket.io')
, RedisStore = require('connect-redis')(express)
, redis = require("redis")
, client = redis.createClient()
, routes = require('./routes');
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({
secret: 'hayaoshi'
, store: new RedisStore()
, cookie: {maxAge: 7 * 24 * 6 * 60 * 60 * 1000 * 2} //2week
}));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
/*
client.set("sample KEY", "ABC 123");
client.get("sample KEY", function(err, replies){
console.log('■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■');
console.log(replies);
console.log('■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■');
});
client.set("string key", "string val", redis.print);
client.hset("hash key", "hashtest 1", "some value", redis.print);
client.hset(["hash key", "hashtest 2", "some other value"], redis.print);
client.hkeys("hash key", function (err, replies) {
console.log(replies.length + " replies:");
replies.forEach(function (reply, i) {
console.log(" " + i + ": " + reply);
});
client.quit();
});
*/
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
//socket
var io = socketio.listen(app);
var count = 0;
var joinList = [];
var questionList = [];
var pushList = [];
var userProfile;
//socket
io.sockets.on('connection', function(socket) {
//connect
count++;
io.sockets.emit('count change', count);
//参加者リスト
var flgSanka = true;
for (var i in joinList){
if(joinList[i].name == userProfile){
flgSanka = false;
break;
}
}
if(flgSanka){
var join = {name:userProfile, plusOne:0, minusOne:0};
joinList.push(join);
}
io.sockets.emit('joinList', joinList);
//メッセージを送る
var tt = new Date().getTime();
var data = {name:userProfile , text: 'ようこそ', time: tt};
io.sockets.emit('new message', data);
//chat
socket.on('new message', function(data){
var tt = new Date().getTime();
var text = data.text;
//text = text.replace(/[!-\/:-@\[-`{-~]/g, "★");
text = text.replace(/</g, "<");
text = text.replace(/>/g, ">");
text = text.replace(/"/g, "”");
text = text.replace(/'/g, "’");
console.log('★★★★★★text ' + text );
var data = {name:data.name , text: text, time: tt};
io.sockets.emit('new message', data);
});
//pushButton
socket.on('pushButton', function(data){
//押した人リスト
var flgPush = true;
for (var i in pushList){
if(pushList[i] == data.name){
flgPush = false;
break;
}
}
if(flgPush){
pushList.push(data.name);
io.sockets.emit('pushButton', data);
}
io.sockets.emit('push');
});
//pushPlus1
socket.on('pushPlusOne', function(data){
for (var i in joinList){
if(pushList[0] == joinList[i].name){
joinList[i].plusOne = joinList[i].plusOne + 1;
break;
}
}
io.sockets.emit('joinList', joinList);
io.sockets.emit('pushList Clear');
io.sockets.emit('pushPlusOne');
pushList.length = 0;
});
//pushMinus1
socket.on('pushMinusOne', function(data){
for (var i in joinList){
if(pushList[0] == joinList[i].name){
joinList[i].minusOne = joinList[i].minusOne + 1;
break;
}
}
io.sockets.emit('joinList', joinList);
io.sockets.emit('pushList Clear');
io.sockets.emit('pushMinusOne');
pushList.length = 0;
});
//NoCount
socket.on('pushNoCount', function(data){
io.sockets.emit('pushList Clear');
pushList.length = 0;
io.sockets.emit('pushNoCount');
});
//pushReset
//score clear
socket.on('pushReset', function(data){
for (var i in joinList){
joinList[i].plusOne = 0;
joinList[i].minusOne = 0;
}
io.sockets.emit('joinList', joinList);
io.sockets.emit('pushList Clear');
io.sockets.emit('pushReset');
pushList.length = 0;
});
//disconnect
socket.on('disconnect', function() {
count--;
socket.broadcast.emit('count change', count);
for (var i in joinList){
if(joinList[i].name == userProfile){
joinList.splice(i,1);
break;
}
}
io.sockets.emit('joinList', joinList);
//メッセージを送る
var tt = new Date().getTime();
var data = {name:userProfile , text: 'さようなら', time: tt};
io.sockets.emit('new message', data);
});
});
// Routes
//app.get('/', routes.index);
app.get('/', function(req, res) {
userProfile = req.session.twitID;
var title = 'node de hayaoshi';
res.render('index', { title:title, twitID: req.session.twitID });
});
app.post('/', function(req, res){
//postで取得してsessionに入れるんだけど、socketでsessionの値を使う方法がわからないので、変数にも入れてる。
req.session.twitID = req.body.name;
userProfile = req.session.twitID;
res.render('index', { title:'index', twitID: req.session.twitID });
});
app.get('/logout', function(req,res){
delete req.session.twitID;
// res.render('index', { title: req.session.twitID, test:'test' });
res.redirect('/');
});
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);