-
Notifications
You must be signed in to change notification settings - Fork 3
/
test-actioncable.js
75 lines (66 loc) · 1.85 KB
/
test-actioncable.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
"use strict"
const Cable = require('es6-actioncable')
const Async = require('async')
const EventEmitter = require('events').EventEmitter
class Websocket {
connect() {
console.log('connecting websocket')
this.consumer = Cable.createConsumer('ws://localhost:3000/cable/', { origin: 'http://localhost:3000' })
}
getConsumer() {
if(!this.consumer) {
this.connect()
}
return this.consumer
}
closeConnection() {
if(this.consumer) {
Cable.endConsumer(this.consumer)
}
delete this.consumer
}
}
const clients = []
class EventsChannel extends EventEmitter {
constructor() {
super()
}
subscribe() {
var _this = this
this.subscription = (new Websocket).getConsumer().subscriptions.create("EventsChannel", {
connected: function () {
clients.push(this)
_this.emit('connected')
},
disconnected: function () {
clients.splice(clients.indexOf(this), 1)
_this.emit('disconnected')
},
received: function (data) {
_this.emit('count', data.count, data.broadcastAt)
}
})
}
unsubscribe() {
if(this.subscription)
this.subscription.unsubscribe()
}
}
var startTime = new Date().getTime()
Async.timesLimit(2500, 50, function(n, next) {
const channel = new EventsChannel()
channel.subscribe()
channel.on('connected', () => {
console.log('Client', n, 'connected')
channel.removeAllListeners('connected')
channel.on('disconnected', () => {
console.log('Client', n, 'disconnected')
})
next()
})
channel.on('count', (count, broadcastAt) => {
console.log(clients.length, 'clients connected,' + count + ' reported by ActionCable. Elapsed time', Math.round(new Date().getTime() - broadcastAt), 'ms')
})
}, function() {
console.log('All clients connected in ', new Date().getTime() - startTime, 'ms')
})