-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathconnection-load.ts
More file actions
executable file
·104 lines (82 loc) · 2.63 KB
/
connection-load.ts
File metadata and controls
executable file
·104 lines (82 loc) · 2.63 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
#!/usr/bin/env npx ts-node --compiler-options {"module":"commonjs"}
import {Convergence, LogLevel, RealTimeString, StringInsertEvent} from "../main";
import * as WebSocket from "ws";
const url = "http://localhost:8080/convergence/default"
Convergence.configureLogging({
root: LogLevel.INFO
});
async function connectAndEditModel(i: number): Promise<void> {
let randomNumber = Math.round(Math.random() * 100000);
let convergenceUsername = 'Convergence User ' + randomNumber;
console.log(`Client ${i} connecting`);
const domain = await Convergence.connectAnonymously(url, convergenceUsername,
{
webSocket: {
factory: (u) => new WebSocket(u, {rejectUnauthorized: false}),
class: WebSocket
},
reconnect: {
autoReconnect: true,
fallbackAuth: (authChallenge) => {
authChallenge.anonymous("dsf");
}
}
});
console.log(`Client ${i} connected`);
// open an data model.
const model = await domain.models().openAutoCreate({
collection: "load-test-2",
id: 'model_' + randomNumber,
data: {
name: "User name " + randomNumber,
firstName: 'User'
}
});
console.log(`Client ${i} opened model`);
// Get the root element in the model.
const data = model.root();
let timeOutRandom = Math.round(2 + (Math.random() * 8)) * 1000;
// Set some data
data.set("firstName", "John" + randomNumber);
data.set("lastName", "Doe" + randomNumber);
// Get the firstName property directly
const firstName = data.elementAt("firstName") as RealTimeString;
// Listen for course grained changes
firstName.on(RealTimeString.Events.VALUE, () => {
console.log(firstName.value());
});
// Listen for course grained changes
firstName.on(RealTimeString.Events.INSERT, (evt: StringInsertEvent) => {
console.log(`characters '${evt.value}' added at position (${evt.index})`)
});
// Mutate the string at random intervals.
setTimeout(() => {
firstName.value("Set First Name " + randomNumber * 2);
}, timeOutRandom);
setTimeout(() => {
firstName.insert(3, "insert3");
}, timeOutRandom + 500);
setTimeout(() => {
firstName.insert(2, "insert2");
}, timeOutRandom + 1000);
setTimeout(() => {
firstName.insert(8, "insert 4");
}, timeOutRandom + 2500);
}
async function sleep(timeInMilliSeconds) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(true);
}, timeInMilliSeconds)
})
}
(async () => {
for (let i = 0; i < 3; i++) {
connectAndEditModel(i).catch(e => console.log(e));
await sleep(300);
}
})();
process.on('SIGINT', () => {
console.log('Exiting all ')
process.exit(0);
});