-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconnect.js
69 lines (59 loc) · 2.38 KB
/
connect.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
module.exports = function (context, api, table, user, connectionId) {
return {
connect: async function () {
var date = new Date().toISOString();
// create the table chat
await table.exec('createTableIfNotExists', 'chat');
// insert the user-conn pair
// insert the user registered
var userEntity = {
PartitionKey: { '_': '_users' },
RowKey: { '_': user },
user: { '_': user },
connectTime: { '_': date },
}
await table.exec('insertOrReplaceEntity', 'chat', userEntity);
// TODO: ask Service for online status
// get top 50 the users
var userQuery = table.query().where("PartitionKey eq '_users'").select('user').top(50);
var users = (await table.exec('queryEntities', 'chat', userQuery, null)).entries.map(i => i.user['_']).sort();
// get top 20 chats for top 10 groups
var userGroupQuery = table.query().where("PartitionKey eq '_usergroups_" + user + "'").select('group').top(10);
var groups = (await table.exec('queryEntities', 'chat', userGroupQuery, null)).entries.map(i => i.group['_']).sort();
// return back the subprotocol and the user authed
context.res = {
headers: {
'sec-websocket-protocol': 'protocol1',
'x-asrs-user-id': user
},
body: {
type: 'connected',
users: users,
user: user,
groups: groups,
connection: connectionId,
}
};
await api.broadcast({
type: 'sync',
event: 'addUser',
user: user,
conn: connectionId,
});
context.log(context.res.body.groups);
},
disconnect: async function () {
// todo: integrate with Azure SignalR
// todo: user can have many connections
var log = user + "(" + connectionId + ") disconnected";
context.log(log);
var response = await api.broadcast({
type: 'log',
text: log,
});
context.res = {
body: response,
};
}
}
};