-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (66 loc) · 2.05 KB
/
index.js
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
"use strict";
import WebSocket from "ws";
// import { toEDNString } from "edn-data";
import { randomUUID } from "crypto";
import transit from "transit-js";
const log = (enthrallme) => console.log(enthrallme);
const tReader = transit.reader("json", {
handlers: {
":": (v) => `${v}`,
},
});
const tWriter = transit.writer();
// const encoded = toEDNString([
// [{ key: "foo/bar" }, { map: [[{ key: "message" }, "Hello, World!"]] }],
// ]);
let mdata = transit.map();
mdata.set(transit.keyword("message"), "Hello World");
let message = [[transit.keyword("foo/bar"), mdata]];
const superSecureCrsfToken = "abcd1234";
const endpoint = `ws://localhost:8080/ws?client-id=${randomUUID()}&csrf-token=${superSecureCrsfToken}`;
const ws = new WebSocket(endpoint);
ws.onopen = (event) => {
log(`Connection opened to '${endpoint}'!`);
ws.send(tWriter.write(message));
};
ws.onmessage = ({ data }) => {
let decoded = tReader.read(data.slice(1)); // sente puts a `-` or a `+` at the start of the payload...we don't want that, otherwise transit decoding will fail!
if ("chsk/ws-ping" === decoded) {
log("I received a chsk/ws-ping!");
} else {
let key = decoded[0];
if (Array.isArray(key)) {
// we are dealing with actual "business-logic" replies...
let payloadType = key[0];
let payload = transit.mapToObject(key[1]);
switch (payloadType) {
case "foo/bar":
log(payload.message);
break;
default:
log(`I received an unknown payloadType '${payloadType}'`);
break;
}
} else {
// we are dealing with typical other `sente` websocket replies...
switch (key) {
case "chsk/handshake":
log(`I've received a chsk/handshake '${key}!'`);
break;
default:
log(`I received an unknown key '${key}'!`);
break;
}
}
}
};
ws.onclose = (event) => {
log(`Connection closed to '${endpoint}'!`);
};
ws.onerror = ({ message }) => {
if (message.includes("403")) {
log("Unauthorized!!");
} else {
log(message);
}
};