-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
223 lines (222 loc) · 8.33 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
var url = require('url');
var nano = require('nano');
var couchdb = nano('https://myiriscouchserver.iriscouch.com');
var formidable = require('formidable');
var BufferedStream = require('bufferedstream');
var couchMapReduce = function (doc) {
emit([doc.room, doc.when], doc);
};
couchdb.db.create('chat', function(err) {
if (err && err.status_code !== 412) {
throw err;
}
couchdb.db.create('users', function(err) {
if (err && err.status_code !== 412) {
throw err;
}
var designDoc = {
language: "javascript",
views: {
by_room: {
map: couchMapReduce.toString()
}
}
};
var chatDB = couchdb.use('chat');
(function insertOrUpdateDesignDoc() {
chatDB.insert(designDoc, '_design/designdoc', function(err) {
if (err) {
if (err.status_code === 409) {
chatDB.get('_design/designdoc', function(err, ddoc) {
if (err) { return console.error(err); }
designDoc._rev = ddoc._rev;
insertOrUpdateDesignDoc();
});
return;
} else {
return console.error(err);
}
}
startServer();
});
}());
});
});
function startServer() {
console.log('starting server');
var chatDB = couchdb.use('chat');
var userDB = couchdb.use('users');
var httpd = require('http').createServer(handler);
var io = require('socket.io').listen(httpd);
var fs = require('fs');
httpd.listen(4000);
function handler(req, res) {
var username;
if (req.method === 'POST' && req.url.indexOf('/avatar') === 0) {
var currentUserDocRev;
console.log('got avatar');
var bufferedRequest = new BufferedStream();
bufferedRequest.headers = req.headers;
bufferedRequest.pause();
req.pipe(bufferedRequest);
// parse username
console.log(url.parse(req.url).query);
username = url.parse(req.url, true).query.username;
userDB.insert({username: username}, username, function(err, user) {
if (err) {
if (err.status_code === 409) {
userDB.get(username, function(err, user) {
if (err) {
console.error(err);
res.writeHead(500);
return res.end(JSON.stringify(err));
}
currentUserDocRev = user._rev;
bufferedRequest.resume();
});
return;
} else {
res.writeHead(500);
return res.end(JSON.stringify(err));
}
}
console.log('username inserted, rev = ', user.rev);
currentUserDocRev = user.rev;
console.log('currentUserDocRev:', currentUserDocRev);
bufferedRequest.resume();
});
// handle avatar upload
var form = new formidable.IncomingForm();
form.encoding = 'utf8';
form.parse(bufferedRequest);
form.onPart = function(part) {
if (part.name !== 'avatar') {
return;
}
var attachment = userDB.attachment.insert(
username, 'avatar', null, part.mime, {rev: currentUserDocRev});
part.pipe(attachment);
attachment.on('error', function(err) {
console.error(err);
res.writeHead(500);
return res.end(JSON.stringify(err));
});
attachment.on('end', function() {
res.end();
});
};
} else if (req.url.indexOf('/avatar') === 0){
// serve the avatar
username = url.parse(req.url, true).query.username;
userDB.attachment.get(username, 'avatar').pipe(res);
} else {
// serve the index page
fs.readFile(__dirname + '/index.html',
function(err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
}
);
}
}
function sendBackLog(socket, room) {
var getOptions = {
start_key: JSON.stringify([room, 9999999999999]),
end_key: JSON.stringify([room, 0]),
limit: 10,
descending: true
};
chatDB.get('_design/designdoc/_view/by_room', getOptions,
function(err, results) {
var messages = results.rows.reverse().map(function(res) {
return res.value;
});
socket.emit('backlog', messages);
});
}
var chat = io.of('/chat');
chat.on('connection', function (socket) {
socket.on('clientMessage', function(content) {
socket.get('username', function(err, username) {
if (! username) {
username = socket.id;
}
socket.get('room', function(err, room) {
if (err) { throw err; }
var broadcast = socket.broadcast;
var message = content;
if (room) {
broadcast.to(room);
}
var messageDoc = {
when: Date.now(),
from: username,
room: room,
message: content
};
socket.emit('serverMessage', messageDoc);
chatDB.insert(messageDoc, function(err) {
if (err) { console.error(err); }
});
broadcast.emit('serverMessage', messageDoc);
});
});
});
socket.on('login', function(username) {
socket.set('username', username, function(err) {
if (err) { throw err; }
var message = {
from: username,
message: 'Logged in',
when: Date.now()
};
socket.emit('serverMessage', message);
socket.broadcast.emit('serverMessage', message);
sendBackLog(socket, null);
});
});
socket.on('disconnect', function() {
socket.get('username', function(err, username) {
if (! username) {
username = socket.id;
}
var message = {
from: username,
message: 'disconnected',
when: Date.now()
};
socket.broadcast.emit('serverMessage', message);
});
});
socket.on('join', function(room) {
socket.get('room', function(err, oldRoom) {
if (err) { throw err; }
socket.set('room', room, function(err) {
if (err) { throw err; }
socket.join(room);
if (oldRoom) {
socket.leave(oldRoom);
}
socket.get('username', function(err, username) {
if (! username) {
username = socket.id;
}
var message = {
from: username,
message: 'joined room ' + room + '. Fetching backlog...',
when: Date.now()
};
socket.emit('serverMessage', message);
socket.broadcast.to(room).emit('serverMessage', message);
});
sendBackLog(socket, room);
});
});
});
socket.emit('login');
});
}