-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathconnect.ts
More file actions
executable file
·108 lines (98 loc) · 3.08 KB
/
connect.ts
File metadata and controls
executable file
·108 lines (98 loc) · 3.08 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
import {
AuthenticatingEvent,
CancellationToken,
ConnectedEvent,
ConnectingEvent,
ConnectionFailedEvent,
ConnectionScheduledEvent,
Convergence,
ConvergenceDomain,
DisconnectedEvent,
ErrorEvent,
IConvergenceOptions,
InterruptedEvent,
LogLevel
} from "../main";
import * as WebSocket from "ws";
import {TypeChecker} from "../main/util/TypeChecker";
Convergence.configureLogging({
root: LogLevel.INFO,
loggers: {
"socket": LogLevel.INFO,
"connection": LogLevel.INFO,
"protocol": LogLevel.INFO,
"heartbeat": LogLevel.ERROR,
"models": LogLevel.INFO,
"storage": LogLevel.WARN,
}
});
// const DOMAIN_URL = "ws://localhost:8000/api/realtime/convergence/default";
const DOMAIN_URL = "ws://localhost:8080/convergence/default";
const DOMAIN_USERNAME = "test";
const DOMAIN_PASSWORD = "password";
const DISPLAY_NAME = "test user";
const OPTIONS: IConvergenceOptions = {
webSocket: {
factory: (u) => new WebSocket(u, {rejectUnauthorized: false}),
class: WebSocket
},
reconnect: {
autoReconnect: true,
// reconnectIntervals: [1],
autoReconnectOnInitial: true,
fallbackAuth: (authChallenge) => {
authChallenge.anonymous(DISPLAY_NAME);
}
},
models: {
data: {
undefinedObjectValues: "error",
undefinedArrayValues: "error"
}
}
};
export function createDomain(options?: IConvergenceOptions): ConvergenceDomain {
options = options || {};
Object.assign(options, OPTIONS);
const domain = new ConvergenceDomain(DOMAIN_URL, options);
domain.events().subscribe((event) => {
if (event instanceof ConnectionScheduledEvent) {
console.log(`Connection scheduled in ${event.delay} seconds`);
} else if (event instanceof ConnectingEvent) {
console.log(`Connecting to: ${domain.url()}`);
} else if (event instanceof ConnectionFailedEvent) {
console.log(`Connection failed: {code: "${event.code}", message: "${event.message}"}`);
} else if (event instanceof ConnectedEvent) {
console.log(`Connected to ${event.domain.namespace()}/${event.domain.id()}`);
} else if (event instanceof AuthenticatingEvent) {
console.log(`Authenticating {method: "${event.method}"}`);
} else if (event instanceof DisconnectedEvent) {
console.log(`Disconnected`);
} else if (event instanceof InterruptedEvent) {
console.log(`Interrupted`);
} else if (event instanceof ErrorEvent) {
console.error(`Error: ${event.message}`, event.cause);
}
}
);
return domain;
}
export function connect(cancellationToken?: CancellationToken, anonymous: boolean = true): Promise<ConvergenceDomain> {
const domain = createDomain();
if (TypeChecker.isSet(cancellationToken)) {
cancellationToken._bind(() => {
domain.disconnect();
});
}
if (anonymous) {
return domain
.connectAnonymously(() => Promise.resolve(DISPLAY_NAME)).then(() => domain);
} else {
return domain
.connectWithPassword(() => Promise.resolve({
username: DOMAIN_USERNAME,
password: DOMAIN_PASSWORD
}))
.then(() => domain);
}
}