-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathwebsockets.js
More file actions
147 lines (130 loc) · 3.85 KB
/
websockets.js
File metadata and controls
147 lines (130 loc) · 3.85 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const WebSocket = require('ws')
const assert = require('chai').assert
const http = require('http')
const parallel = require('run-parallel')
const SolidWs = require('../')
const utils = require('./utils')
describe('Solid-ws', function () {
const server = http.createServer()
const port = 8000
const pubsub = SolidWs(server)
function check (msgs, uris, done) {
parallel(msgs.map(function (msg, i) {
return function (cb) {
assert.equal(msg.split(' ')[0], 'ack')
const index = uris.indexOf(msg.split(' ')[1])
assert.notEqual(index, -1, 'URL not found')
uris.splice(index, 1)
cb()
}
}),
function () {
assert.equal(uris.length, 0)
done()
})
}
before(function (done) {
server.listen(port, function (err) {
done(err)
})
})
after(function () {
server.close()
})
describe('sub', function () {
beforeEach(function (done) {
let client = new WebSocket('http://localhost:' + port)
client.on('open', done)
})
afterEach(function (done) {
client.close()
done()
})
it('should receive ack in the form `ack $uri`', function (done) {
const uri = 'http://example.com/myresource'
client.send('sub ' + uri)
client.on('message', function (msg) {
assert.equal(msg, 'ack ' + uri)
done()
})
})
it('should receive ack for any resource given', function (done) {
const uris = [
'http://example.com/hello',
'http://example.com/hello/hello.ttl',
'http://example.com/hello/hello/.acl']
uris.map(function (uri) {
client.send('sub ' + uri)
})
const msgs = []
client.on('message', function (msg) {
msgs.push(msg)
if (msgs.length === uris.length) {
check(msgs, uris, done)
}
})
})
it('should receive ack even if has already subscribed', function (done) {
const uris = [
'http://example.com/hello',
'http://example.com/hello',
'http://example.com/hello/hello.ttl',
'http://example.com/hello/hello.ttl',
'http://example.com/hello/hello/.acl',
'http://example.com/hello/hello/.acl']
uris.map(function (uri) {
client.send('sub ' + uri)
})
const msgs = []
client.on('message', function (msg) {
msgs.push(msg)
if (msgs.length === uris.length) {
check(msgs, uris, done)
}
})
})
})
describe('pub', function () {
it('should pub to everyone, independently of the host name', function (done) {
const urls = [
'http://example.com/resource.ttl',
'http://domain.com/resource.ttl',
'/resource.ttl']
const users = [
'http://nicola.io/#me',
'http://timbl.com/#me']
const clients = users.map(function () {
return new WebSocket('http://localhost:' + port)
})
const pubs = []
utils.connectAll(clients, urls, function () {
utils.ackAll(clients, function () {
utils.pubAll(clients, pubs, function () {
assert.equal(pubs.length, users.length)
done()
})
pubsub.publish('/resource.ttl')
})
})
})
it('should be received by all the clients subscribed to a resource', function (done) {
const url = 'http://example.com/resource.ttl'
const users = [
'http://nicola.io/#me',
'http://timbl.com/#me']
const clients = users.map(function () {
return new WebSocket('http://localhost:' + port)
})
const pubs = []
utils.connectAll(clients, url, function () {
utils.ackAll(clients, function () {
utils.pubAll(clients, pubs, function () {
assert.equal(pubs.length, users.length)
done()
})
pubsub.publish('/resource.ttl')
})
})
})
})
})