|
40 | 40 | ##### **`HOW TO TRIGGER`**
|
41 | 41 | ```js
|
42 | 42 | wb.notify(target, {"test": 123456});
|
| 43 | +``` |
| 44 | + |
| 45 | +# **WEB SETUP** |
| 46 | +```js |
| 47 | +// target can be random string or current token |
| 48 | +socket = new WebSocket(`ws://${window.location.host}/ws/socket-server/{{target}}`) |
| 49 | +socket.onmessage = (event) => { |
| 50 | + console.log(event); |
| 51 | + // your event handler |
| 52 | + data = JSON.parse(event.data); |
| 53 | + switch(data.type) { |
| 54 | + case "connect": |
| 55 | + // code block |
| 56 | + break; |
| 57 | + case "your-custom-type": |
| 58 | + // code block |
| 59 | + break; |
| 60 | + default: |
| 61 | + // code block |
| 62 | + } |
| 63 | +} |
| 64 | +// notify backend |
| 65 | +socket.send(JSON.stringify({"message": "test"})) |
| 66 | +``` |
| 67 | +## Example connection via `token` |
| 68 | +```js |
| 69 | +socket = new WebSocket(`ws://${window.location.host}/ws/socket-server/276a40aec1dffc48a25463c3e2545473b45a663364adf3a2f523b903aa254c9f`) |
| 70 | +// if token is valid, it's session will be connected to the user's master jid |
| 71 | +// all notification from FE will now be send to user's master jid |
| 72 | + |
| 73 | +socket.onmessage = (event) => { |
| 74 | + console.log(event); |
| 75 | +} |
| 76 | + |
| 77 | +// all clients that is subscribed to user's master jid will received this notification |
| 78 | +socket.send(JSON.stringify({"message": "test"})) |
| 79 | +``` |
| 80 | +#### console.`log`(**event**) |
| 81 | +> ") |
| 82 | +#### event.`data` |
| 83 | +```txt |
| 84 | +{"type": "connect", "authenticated": true, "session_id": null} |
| 85 | +``` |
| 86 | +--- |
| 87 | +## Example connection **without** `token` |
| 88 | +```js |
| 89 | +socket = new WebSocket(`ws://${window.location.host}/ws/socket-server/any-ranmdom-string`) |
| 90 | +// this socket will be subscribed to a random uuid |
| 91 | +// you will need to use that random uuid on wb.notify as target params |
| 92 | +// FE is required to send it on walkers with wb.notify to override target |
| 93 | + |
| 94 | +socket.onmessage = (event) => { |
| 95 | + console.log(event); |
| 96 | + // you may get session_id from JSON.parse(event.data).session_id |
| 97 | +} |
| 98 | + |
| 99 | +// not advisable but still can be used for notifying that random uuid |
| 100 | +socket.send(JSON.stringify({"message": "test"})) |
| 101 | +``` |
| 102 | +#### console.`log`(**event**) |
| 103 | +> ") |
| 104 | +#### event.`data` |
| 105 | +```txt |
| 106 | +{"type": "connect", "authenticated": false, "session_id": "53a0e05e-8689-4e87-984d-dbd4bad58c9d"} |
43 | 107 | ```
|
0 commit comments