-
Notifications
You must be signed in to change notification settings - Fork 517
/
io.js
44 lines (37 loc) · 1.19 KB
/
io.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
/**
* Created by Hongcai Deng on 2015/12/28.
*/
'use strict';
let shortid = require('shortid');
let mailin = require('./mailin');
let onlines = new Map();
module.exports = function(io) {
mailin.on('message', function(connection, data) {
let to = data.headers.to.toLowerCase();
let exp = /[\w\._\-\+]+@[\w\._\-\+]+/i;
if(exp.test(to)) {
let matches = to.match(exp);
let shortid = matches[0].substring(0, matches[0].indexOf('@'));
if(onlines.has(shortid)) {
onlines.get(shortid).emit('mail', data);
}
}
});
io.on('connection', socket => {
socket.on('request shortid', function() {
onlines.delete(socket.shortid);
socket.shortid = shortid.generate().toLowerCase(); // generate shortid for a request
onlines.set(socket.shortid, socket); // add incomming connection to online table
socket.emit('shortid', socket.shortid);
});
socket.on('set shortid', function(id) {
onlines.delete(socket.shortid);
socket.shortid = id;
onlines.set(socket.shortid, socket);
socket.emit('shortid', socket.shortid);
})
socket.on('disconnect', socket => {
onlines.delete(socket.shortid);
});
});
};