@casual-simulation/websocket 3.5.3-alpha.16326443512 → 3.10.2
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/README.md +58 -92
- package/package.json +41 -42
package/README.md
CHANGED
|
@@ -1,121 +1,87 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @casual-simulation/websocket
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
[](https://travis-ci.org/developit/undom)
|
|
3
|
+
A lightweight WebSocket management library that provides automatic reconnection capabilities and connection lifecycle management using RxJS observables.
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
## Overview
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
>
|
|
10
|
-
> `1kB`, works in Node and browsers, plugins coming soon!
|
|
7
|
+
This package provides two main classes for managing WebSocket connections with built-in reconnection logic and observable-based event handling:
|
|
11
8
|
|
|
12
|
-
|
|
9
|
+
### `ReconnectableSocket`
|
|
13
10
|
|
|
14
|
-
|
|
11
|
+
A WebSocket wrapper that provides automatic reconnection capabilities and exposes connection events through RxJS observables.
|
|
15
12
|
|
|
16
|
-
|
|
13
|
+
**Features:**
|
|
17
14
|
|
|
18
|
-
|
|
15
|
+
- Wraps native WebSocket API with observable-based event handling
|
|
16
|
+
- Distinguishes between intentional disconnections and unexpected connection losses
|
|
17
|
+
- Provides `onOpen`, `onClose`, `onMessage`, and `onError` observables
|
|
18
|
+
- Manual control over opening and closing connections
|
|
19
|
+
- Supports WebSocket protocols
|
|
19
20
|
|
|
20
|
-
|
|
21
|
+
**Usage:**
|
|
21
22
|
|
|
22
|
-
|
|
23
|
+
```typescript
|
|
24
|
+
const socket = new ReconnectableSocket('wss://example.com', 'protocol');
|
|
23
25
|
|
|
24
|
-
|
|
26
|
+
socket.onOpen.subscribe(() => {
|
|
27
|
+
console.log('Connected!');
|
|
28
|
+
});
|
|
25
29
|
|
|
26
|
-
|
|
30
|
+
socket.onMessage.subscribe((event) => {
|
|
31
|
+
console.log('Message received:', event.data);
|
|
32
|
+
});
|
|
27
33
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
34
|
+
socket.onClose.subscribe((reason) => {
|
|
35
|
+
if (reason.type === 'closed') {
|
|
36
|
+
console.log('Intentionally disconnected');
|
|
37
|
+
} else {
|
|
38
|
+
console.log('Connection lost:', reason.reason);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
31
41
|
|
|
32
|
-
|
|
42
|
+
socket.open();
|
|
43
|
+
socket.send('Hello, server!');
|
|
44
|
+
```
|
|
33
45
|
|
|
34
|
-
|
|
46
|
+
### `SocketManager`
|
|
35
47
|
|
|
36
|
-
|
|
48
|
+
A higher-level WebSocket connection manager that automatically handles reconnection attempts after unexpected disconnections.
|
|
37
49
|
|
|
38
|
-
|
|
50
|
+
**Features:**
|
|
39
51
|
|
|
40
|
-
|
|
52
|
+
- Manages `ReconnectableSocket` lifecycle
|
|
53
|
+
- Automatic reconnection with configurable delay (default: 5 seconds)
|
|
54
|
+
- Connection state tracking via observable
|
|
55
|
+
- Manual offline mode control
|
|
56
|
+
- Debounced reconnection to prevent rapid retry attempts
|
|
41
57
|
|
|
42
|
-
|
|
43
|
-
require('undom/register');
|
|
58
|
+
**Usage:**
|
|
44
59
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
```
|
|
60
|
+
```typescript
|
|
61
|
+
const manager = new SocketManager('wss://example.com', 'protocol');
|
|
48
62
|
|
|
49
|
-
|
|
63
|
+
manager.connectionStateChanged.subscribe((connected) => {
|
|
64
|
+
console.log(connected ? 'Online' : 'Offline');
|
|
65
|
+
});
|
|
50
66
|
|
|
51
|
-
|
|
67
|
+
manager.init();
|
|
52
68
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
import undom from 'undom';
|
|
69
|
+
// Access underlying socket
|
|
70
|
+
manager.socket.send('Hello!');
|
|
56
71
|
|
|
57
|
-
|
|
72
|
+
// Force offline mode
|
|
73
|
+
manager.forcedOffline = true;
|
|
58
74
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
document.body.appendChild(foo);
|
|
75
|
+
// Toggle offline mode
|
|
76
|
+
manager.toggleForceOffline();
|
|
62
77
|
```
|
|
63
78
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
## Recipe: Serialize to HTML
|
|
67
|
-
|
|
68
|
-
One task `undom` doesn't handle for you by default is HTML serialization. A proper implementation of this would be cumbersome to maintain and would rely heavily on getters and setters, which limits browser support. Below is a simple recipe for serializing an `undom` Element (Document, etc) to HTML.
|
|
69
|
-
|
|
70
|
-
#### Small & in ES2015:
|
|
71
|
-
|
|
72
|
-
```js
|
|
73
|
-
Element.prototype.toString = function () {
|
|
74
|
-
return serialize(this);
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
function serialize(el) {
|
|
78
|
-
return el.nodeType == 3
|
|
79
|
-
? enc(el.nodeValue)
|
|
80
|
-
: '<' +
|
|
81
|
-
this.nodeName.toLowerCase() +
|
|
82
|
-
this.attributes.map(attr).join('') +
|
|
83
|
-
'>' +
|
|
84
|
-
this.childNodes.map(serialize).join('') +
|
|
85
|
-
'</' +
|
|
86
|
-
this.nodeName.toLowerCase() +
|
|
87
|
-
'>';
|
|
88
|
-
}
|
|
89
|
-
let attr = (a) => ` ${a.name}="${enc(a.value)}"`;
|
|
90
|
-
let enc = (s) => s.replace(/[&'"<>]/g, (a) => `&#${a.codePointAt(0)};`);
|
|
91
|
-
```
|
|
79
|
+
## Installation
|
|
92
80
|
|
|
93
|
-
|
|
81
|
+
```bash
|
|
82
|
+
npm install @casual-simulation/websocket
|
|
83
|
+
```
|
|
94
84
|
|
|
95
|
-
|
|
85
|
+
## Dependencies
|
|
96
86
|
|
|
97
|
-
|
|
98
|
-
function serialize(el) {
|
|
99
|
-
if (el.nodeType === 3) return el.textContent;
|
|
100
|
-
var name = String(el.nodeName).toLowerCase(),
|
|
101
|
-
str = '<' + name,
|
|
102
|
-
c,
|
|
103
|
-
i;
|
|
104
|
-
for (i = 0; i < el.attributes.length; i++) {
|
|
105
|
-
str +=
|
|
106
|
-
' ' + el.attributes[i].name + '="' + el.attributes[i].value + '"';
|
|
107
|
-
}
|
|
108
|
-
str += '>';
|
|
109
|
-
for (i = 0; i < el.childNodes.length; i++) {
|
|
110
|
-
c = serialize(el.childNodes[i]);
|
|
111
|
-
if (c) str += '\n\t' + c.replace(/\n/g, '\n\t');
|
|
112
|
-
}
|
|
113
|
-
return str + (c ? '\n' : '') + '</' + name + '>';
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function enc(s) {
|
|
117
|
-
return s.replace(/[&'"<>]/g, function (a) {
|
|
118
|
-
return `&#${a};`;
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
```
|
|
87
|
+
- **rxjs**: For observable-based event handling and stream operators
|
package/package.json
CHANGED
|
@@ -1,43 +1,42 @@
|
|
|
1
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
|
-
}
|
|
2
|
+
"name": "@casual-simulation/websocket",
|
|
3
|
+
"version": "3.10.2",
|
|
4
|
+
"description": "Minimal WebSocket implementation",
|
|
5
|
+
"keywords": [],
|
|
6
|
+
"author": "Casual Simulation, Inc.",
|
|
7
|
+
"homepage": "https://github.com/casual-simulation/casualos",
|
|
8
|
+
"license": "AGPL-3.0-only",
|
|
9
|
+
"main": "index.js",
|
|
10
|
+
"types": "index.d.ts",
|
|
11
|
+
"module": "index",
|
|
12
|
+
"directories": {
|
|
13
|
+
"lib": "."
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"/README.md",
|
|
17
|
+
"/LICENSE.txt",
|
|
18
|
+
"**/*.js",
|
|
19
|
+
"**/*.js.map",
|
|
20
|
+
"**/*.d.ts"
|
|
21
|
+
],
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/casual-simulation/casualos.git"
|
|
25
|
+
},
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/casual-simulation/casualos/issues"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"rxjs": "8.0.0-alpha.14"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"watch": "tsc --watch",
|
|
37
|
+
"watch:player": "npm run watch",
|
|
38
|
+
"build": "echo \"Nothing to do.\"",
|
|
39
|
+
"test": "jest",
|
|
40
|
+
"test:watch": "jest --watchAll"
|
|
41
|
+
}
|
|
42
|
+
}
|