-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
64 lines (54 loc) · 1.59 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
var io = require('socket.io'),
express = require('express'),
messages = [],
messageType = {'GET_DATA' : 0, 'STRING' : 1} ; // GET=取得要求、 STRING=チャットメッセージ
var app = express.createServer();
app.configure(function(){
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/public'));
app.set("view options", { layout: false });
app.get('/', function(req, res) {
res.render('index.ejs');
});
});
app.listen(8000);
var socket = io.listen(app);
socket.on('connection', function(client){
client.on('message', function(msg) {onMessage(msg, client);});
client.on('disconnect', function() {onDisconnect(client)});
});
function addMessage(msg)
{
msg.id = messages.length + 1;
msg.createdAt = getNowDate();
if (msg.option.color == '') {
msg.option.color = 'black';
}
messages.push(msg);
return msg;
}
function onDisconnect(client)
{
msg = {"data" : client. sessionId+"が切断されました",
"type" : messageType.STGING,
"option" : {"color" : "red"},
"createdAt" : getNowDate()} ;
socket.broadcast(msg);
}
function onMessage(msg, client)
{
if (msg.type == messageType.GET_DATA) {
if (typeof msg.more_id == 'number') {
client.send(messages.slice(msg.more_id));
}
} else {
addMessage(msg);
client.send(msg);
client.broadcast(msg);
}
}
function getNowDate(){
d = new Date();
return d.getFullYear() + "-" + (d.getMonth()+1) + "-" + d.getDate()+ " "+ d.getHours()+':' +d.getMinutes()+':' + d.getSeconds();
}
//}}}