-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
presence.coffee
48 lines (42 loc) · 1.64 KB
/
presence.coffee
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
import {Presence, PresenceStream} from '/lib/presence'
import {Rooms} from '/lib/rooms'
import {pulseFrequency, logPresencePulse} from './log'
PresenceStream.allowRead 'all' # anyone can read if they know channel ID
PresenceStream.allowWrite 'none' # all messages from server
## On server (re)start, remove old presence values; if they're not expired,
## they will reload and reconnect.
Presence.remove {}
Rooms.update {},
$set:
joined: []
adminVisit: false
,
multi: true
## Mapping from Meteor connection id to presenceId [server only]
## Used in server/presence.coffee to destroy presence upon disconnect.
connections = {}
## Set presenceId corresponding to a connectionId
## (Called in lib/presence.coffee)
export setConnection = (connectionId, presenceId) ->
## Easy case: no change
return if connections[connectionId]?.presenceId == presenceId
## Remove existing connection if we're re-using the same connectionId
closeConnection connectionId if connections[connectionId]?
## Set data and start timer
connections[connectionId] =
presenceId: presenceId
timer:
Meteor.setInterval ->
logPresencePulse presenceId
, pulseFrequency
closeConnection = (connectionId) ->
connection = connections[connectionId]
return unless connection?
delete connections[connectionId]
Meteor.clearInterval connection.timer
Meteor.call 'presenceRemove', connection.presenceId
## Detect closed DDP connections, based on a very small piece of
## https://github.com/Meteor-Community-Packages/meteor-user-status/blob/master/server/status.js
Meteor.onConnection (connection) ->
connection.onClose ->
closeConnection connection.id