-
Notifications
You must be signed in to change notification settings - Fork 15
/
demo.js
54 lines (48 loc) · 1.43 KB
/
demo.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
/* global EventSource */
var server = 'http://catlobby.maxogden.com'
var nets = require('nets')
var url = require('url')
var params = url.parse(window.location.href, true).query
if (params.room) {
var room = params.room
// listen for pongs
var events = new EventSource(server + '/v1/' + room + '/pongs')
events.onmessage = function onMessage (e) {
var row
try {
row = JSON.parse(e.data)
} catch (e) {
row = {}
}
console.log('pongs onmessage', row)
}
setInterval(function () {
nets({method: 'POST', url: server + '/v1/' + room + '/ping', json: {time: new Date()}}, function (err, resp, body) {
if (err) console.error(err)
console.log('SENT PING', body)
})
}, 5000)
} else {
nets({method: 'POST', url: server + '/v1', json: true}, function (err, resp, body) {
if (err) console.error(err)
var room = body.name
console.log('ROOM', room)
// listen for pings
var events = new EventSource(server + '/v1/' + room + '/pings')
events.onmessage = function onMessage (e) {
var row
try {
row = JSON.parse(e.data)
} catch (e) {
row = {}
}
console.log('pings onmessage', row)
}
setInterval(function () {
nets({method: 'POST', url: server + '/v1/' + room + '/pong', json: {time: new Date()}}, function (err, resp, body) {
if (err) console.error(err)
console.log('SENT PONG', body)
})
}, 5000)
})
}