-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathwebsockets.js
More file actions
152 lines (131 loc) · 3.83 KB
/
websockets.js
File metadata and controls
152 lines (131 loc) · 3.83 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
148
149
150
151
152
var WebSocket = require('ws')
var assert = require('chai').assert
var http = require('http')
var parallel = require('run-parallel')
var SolidWs = require('../')
var EventEmitter = require('events').EventEmitter
var utils = require('./utils')
describe('Solid-ws', function() {
var server = http.createServer()
var port = 8000
var pubsub = SolidWs(server)
function check(msgs, uris, done) {
parallel(msgs.map(function (msg, i) {
return function(cb) {
assert.equal(msg.split(' ')[0], 'ack')
var 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) {
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) {
var 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) {
var 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)
})
var 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) {
var 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)
})
var 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) {
var urls = [
'http://example.com/resource.ttl',
'http://domain.com/resource.ttl',
'/resource.ttl' ]
var users = [
'http://nicola.io/#me',
'http://timbl.com/#me' ]
var clients = users.map(function() {
return new WebSocket('http://localhost:' + port)
})
var 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) {
var url = 'http://example.com/resource.ttl'
var users = [
'http://nicola.io/#me',
'http://timbl.com/#me' ]
var clients = users.map(function() {
return new WebSocket('http://localhost:' + port)
})
var 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')
})
})
})
})
})