-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-starter.html
234 lines (200 loc) · 6.38 KB
/
simple-starter.html
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.row {
margin: 15px 0;
}
.col {
display: inline-block;
width: 49%;
}
</style>
<!-- <script src="https://download.kinvey.com/js/kinvey-html5-sdk-3.9.0.js"></script> -->
<script src="/html5-sdk/dist/kinvey-html5-sdk.js"></script>
</head>
<body>
<div class="col">
<div class="row stream">
<div class="row">
<h4>User-to-user communication</h4>
</div>
<div class="row">
<button onclick="initLiveService()">Init LiveService</button>
</div>
<div class="row">
<button onclick="getAllSubstreams()">Get substreams</button>
<button onclick="getACL('594a76b3fb78dc85415757b2')">Get ACL for username</button>
<button onclick="getACL('5989dcc67d3326c22a5a64d7')">Get ACL for admin</button>
</div>
<div class="row">
<button onclick="send('594a76b3fb78dc85415757b2')">Send to username</button>
<button onclick="send('5989dcc67d3326c22a5a64d7')">Send to admin</button>
</div>
<div class="row">
<button onclick="subFor('594a76b3fb78dc85415757b2')">Subscribe for username</button>
<button onclick="subFor('5989dcc67d3326c22a5a64d7')">Subscribe for admin</button>
</div>
<div class="row">
<button onclick="unsubFor('594a76b3fb78dc85415757b2')">Unsub from username</button>
<button onclick="unsubFor('5989dcc67d3326c22a5a64d7')">Unsub from admin</button>
</div>
<div class="row">
<button onclick="subForWrongChannel()">Sub for wrong channel</button>
</div>
<div class="row">
<button onclick="logIn()">Log in</button>
<button onclick="logOut()">Log out</button>
</div>
</div>
</div>
<div class="col">
<div class="row collection">
<div class="row">
<h4>Collections</h4>
</div>
<div class="row">
<button onclick="subscribe()">Subscribe</button>
<button onclick="unsubscribe()">Unsubscribe</button>
</div>
<div class="row">
<button onclick="attachStatusEventsListener()">Listen for Status events</button>
</div>
<div class="row">
<button onclick="createItem()">Create item</button>
</div>
</div>
</div>
<script>
let collection;
function createItem() {
collection.save({ test: new Date() });
}
function subscribe() {
collection.subscribe({
onMessage: (m) => console.log('collection update:', m),
onStatus: (m) => console.log('status:', m),
onError: (m) => console.log('error', m)
})
.then((resp) => {
console.log('subbed', resp);
})
.catch(err => console.error(err));
}
function unsubscribe() {
collection.unsubscribe()
.then((resp) => {
console.log('UNsubbed', resp);
})
.catch(err => console.error(err));
}
</script>
<script>
let stream;
function send(id) {
stream.send(id, { sentBy: userCreds.username })
.then((sendResp) => {
console.log('sent to ' + id);
})
.catch((sendErr) => {
console.log('send err: ', sendErr);
});
}
function subForWrongChannel() {
stream.follow('1234567', {})
.catch(e => console.log(e));;
}
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
let regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
function logIn() {
Kinvey.User.login(userCreds)
.catch(e => console.log(e));
}
function logOut() {
Kinvey.User.logout()
.catch(e => console.log(e));
}
function initLiveService() {
Kinvey.User.getActiveUser()
.registerForLiveService()
.then(() => console.log('registered for live service'))
.catch(err => console.error(err));
}
function subFor(userId) {
stream.follow(userId, {
onMessage: (msg) => console.log('received msg', msg),
onStatus: (status) => console.log('status', status),
onError: (err) => console.error('err', err)
})
.then(() => {
console.log('listening for ' + otherUserId);
})
.catch(err => console.error(err));
}
function unsubFor(userId) {
stream.unfollow(userId)
.then(() => {
console.log('stopped listening for ' + otherUserId);
})
.catch(err => console.error(err));
}
function getAllSubstreams() {
stream.getSubstreams()
.then((substreams) => {
console.log('substreams', substreams);
})
.catch(err => console.error(err));
}
function getACL(userId) {
stream.getACL(userId)
.then(acl => console.log('acl', acl))
.catch(err => console.error(err));
}
function attachStatusEventsListener() {
Kinvey.LiveService.onConnectionStatusUpdates((status) => {
console.log('status event: ', status);
});
}
const otherUserId = getParameterByName('u') != '1' ? '5989dcc67d3326c22a5a64d7' : '594a76b3fb78dc85415757b2';
const userCreds = getParameterByName('a') == '1' ? { username: 'admin', password: 'admin' } : { username: 'username', password: 'qweqwe' };
let currUser;
Kinvey.initialize({
appKey: 'kid_SJsSMfnGZ',
appSecret: 'ea0cc05888be46b2b1ff729b2f7de3d8'
})
.then((activeUser) => {
let promise = Promise.resolve(activeUser);
if (!activeUser) {
promise = Kinvey.User.login(userCreds);
}
return promise;
})
.then((activeUser) => {
currUser = activeUser;
stream = new Kinvey.LiveService.Stream('Test');
collection = Kinvey.DataStore.collection('Events', Kinvey.DataStoreType.Network);
})
.then(() => {
if (currUser._id !== '594a76b3fb78dc85415757b2') {
return;
}
const acl = {
subscribe: [currUser._id, otherUserId],
publish: [currUser._id, otherUserId]
};
return stream.setACL(currUser._id, new Kinvey.LiveService.Stream.StreamACL(acl));
})
.catch((err) => {
console.error(err);
});
</script>
</body>
</html>