@aicore/cocodb-ws-client 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/utils/client.js +17 -4
package/package.json
CHANGED
package/src/utils/client.js
CHANGED
|
@@ -2,20 +2,26 @@ import {WS} from "./WebSocket.js";
|
|
|
2
2
|
import {isString, isObject, isStringEmpty, COCO_DB_FUNCTIONS} from "@aicore/libcommonutils";
|
|
3
3
|
|
|
4
4
|
let client = null;
|
|
5
|
-
const WEBSOCKET_ENDPOINT_COCO_DB = '/ws';
|
|
5
|
+
const WEBSOCKET_ENDPOINT_COCO_DB = '/ws/';
|
|
6
6
|
const ID_TO_RESOLVE_REJECT_MAP = {};
|
|
7
7
|
let id = 0;
|
|
8
8
|
|
|
9
9
|
// @INCLUDE_IN_API_DOCS
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* It creates a websocket connection to the cocoDbServiceEndPoint and listens for messages
|
|
13
|
+
* @param {string} cocoDbServiceEndPoint - The URL of the coco-db service.
|
|
14
|
+
* @param {string} authKey - The authKey is a base64 encoded string of the username and password.
|
|
15
|
+
*/
|
|
11
16
|
export function init(cocoDbServiceEndPoint, authKey) {
|
|
12
|
-
if (isStringEmpty(cocoDbServiceEndPoint))
|
|
17
|
+
if (isStringEmpty(cocoDbServiceEndPoint) || !(cocoDbServiceEndPoint.startsWith('ws://')
|
|
18
|
+
|| cocoDbServiceEndPoint.startsWith('wss://'))) {
|
|
13
19
|
throw new Error('Please provide valid cocoDbServiceEndPoint');
|
|
14
20
|
}
|
|
15
21
|
if (isStringEmpty(authKey)) {
|
|
16
22
|
throw new Error('Please provide valid authKey');
|
|
17
23
|
}
|
|
18
|
-
client = new WS.WebSocket(
|
|
24
|
+
client = new WS.WebSocket(cocoDbServiceEndPoint.trim() + WEBSOCKET_ENDPOINT_COCO_DB, {
|
|
19
25
|
perMessageDeflate: false,
|
|
20
26
|
headers: {
|
|
21
27
|
Authorization: `Basic ${authKey}`
|
|
@@ -26,7 +32,6 @@ export function init(cocoDbServiceEndPoint, authKey) {
|
|
|
26
32
|
});
|
|
27
33
|
|
|
28
34
|
client.on('message', function message(data) {
|
|
29
|
-
console.log('received: %s', data);
|
|
30
35
|
__receiveMessage(data);
|
|
31
36
|
});
|
|
32
37
|
client.on('close', function terminate() {
|
|
@@ -41,6 +46,10 @@ export function init(cocoDbServiceEndPoint, authKey) {
|
|
|
41
46
|
});
|
|
42
47
|
}
|
|
43
48
|
|
|
49
|
+
/**
|
|
50
|
+
* It closes the connection to the server
|
|
51
|
+
* @returns The function close() is being returned.
|
|
52
|
+
*/
|
|
44
53
|
export function close() {
|
|
45
54
|
if (!client) {
|
|
46
55
|
return;
|
|
@@ -48,6 +57,10 @@ export function close() {
|
|
|
48
57
|
client.terminate();
|
|
49
58
|
}
|
|
50
59
|
|
|
60
|
+
/**
|
|
61
|
+
* It returns a string representation of the next integer in a sequence
|
|
62
|
+
* @returns {string} A function that increments the id variable and returns the new value as a hexadecimal string.
|
|
63
|
+
*/
|
|
51
64
|
function getId() {
|
|
52
65
|
id++;
|
|
53
66
|
return id.toString(16);
|