itty-sockets 0.4.0 → 0.5.0
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 +7 -3
- package/connect.d.ts +0 -1
- package/connect.js +1 -1
- package/connect.mjs +1 -1
- package/index.js +1 -1
- package/index.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -66,15 +66,17 @@ import { connect } from 'itty-sockets'
|
|
|
66
66
|
...or simply paste this into your environment/console:
|
|
67
67
|
<!-- BEGIN SNIPPET -->
|
|
68
68
|
```ts
|
|
69
|
-
let connect=(e,s={})=>{let o,t=[],n=0,a={},r=()=>(o||(o=new WebSocket(
|
|
69
|
+
let connect=(e,s={})=>{let o,t=[],n=0,a={},r=()=>(o||(o=new WebSocket(/^(wss?):/.test(e)?e:`wss://ittysockets.io/c/${e}?`+new URLSearchParams(s)),o.onopen=()=>{for(;t.length;)o?.send(t.shift());for(let e of a.open??[])e();n&&o?.close()},o.onmessage=(e,s=JSON.parse(e.data))=>{for(let e of a[s.type??"message"]??[])e({...s,date:new Date(s.date)})},o.onclose=()=>{n=0,o=null;for(let e of a.close??[])e()}),l);const l=new Proxy(r,{get:(e,s)=>({open:r,close:()=>(1==o?.readyState?o.close():n=1,l),send:(e,s)=>(e=JSON.stringify(e),e=s?`@@${s}@@${e}`:e,1==o?.readyState?(o.send(e),l):(t.push(e),r())),push:(e,s)=>(n=1,l.send(e,s)),on:(e,s)=>((a[e]??=[]).push(s),r()),remove:(e,s,o=a[e],t=o?.indexOf(s)??-1)=>(~t&&o?.splice(t,1),r())}[s])});return l};
|
|
70
70
|
```
|
|
71
71
|
<!-- END SNIPPET -->
|
|
72
72
|
|
|
73
73
|
<br />
|
|
74
74
|
|
|
75
|
-
### 2.
|
|
75
|
+
### 2. Connect to a Channel (or external server)
|
|
76
76
|
To start, simply connect to a channel based on a unique name (this can be anything).
|
|
77
77
|
|
|
78
|
+
> **NOTE:** Pass a valid `ws://` or `wss://` URL as the channel identifier to bypass the public [ittysockets.io](https://ittysockets.io) service and use your own.
|
|
79
|
+
|
|
78
80
|
```ts
|
|
79
81
|
import { connect } from 'itty-sockets'
|
|
80
82
|
|
|
@@ -87,6 +89,9 @@ const channel = connect('my-channels/my-super-secret-channel', {
|
|
|
87
89
|
announce: true,
|
|
88
90
|
echo: true
|
|
89
91
|
})
|
|
92
|
+
|
|
93
|
+
// an external server
|
|
94
|
+
const channel = connect('wss://somewhere.else/entirely')
|
|
90
95
|
```
|
|
91
96
|
|
|
92
97
|
#### Connection Options
|
|
@@ -96,7 +101,6 @@ const channel = connect('my-channels/my-super-secret-channel', {
|
|
|
96
101
|
| `{ alias: 'any-string' }` | `undefined` | An optional display name to be included in your messages. |
|
|
97
102
|
| `{ as: 'any-string' }` | `undefined` | An optional display name to be included in your message (same as alias). |
|
|
98
103
|
| `{ announce: true }` | `false` | Shares your uid/alias when joining/leaving. |
|
|
99
|
-
| `{ base: string }` | `undefined` | Define your own server url (requires API compatibility). |
|
|
100
104
|
| `{ echo: true }` | `false` | Echos messages back to original sender (good for testing). |
|
|
101
105
|
|
|
102
106
|
<br />
|
package/connect.d.ts
CHANGED
package/connect.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";exports.connect=(e,s={})=>{let o
|
|
1
|
+
"use strict";exports.connect=(e,s={})=>{let t,o=[],n=0,r={},a=()=>(t||(t=new WebSocket(/^(wss?):/.test(e)?e:`wss://ittysockets.io/c/${e}?`+new URLSearchParams(s)),t.onopen=()=>{for(;o.length;)t?.send(o.shift());for(let e of r.open??[])e();n&&t?.close()},t.onmessage=(e,s=JSON.parse(e.data))=>{for(let e of r[s.type??"message"]??[])e({...s,date:new Date(s.date)})},t.onclose=()=>{n=0,t=null;for(let e of r.close??[])e()}),c);const c=new Proxy(a,{get:(e,s)=>({open:a,close:()=>(1==t?.readyState?t.close():n=1,c),send:(e,s)=>(e=JSON.stringify(e),e=s?`@@${s}@@${e}`:e,1==t?.readyState?(t.send(e),c):(o.push(e),a())),push:(e,s)=>(n=1,c.send(e,s)),on:(e,s)=>((r[e]??=[]).push(s),a()),remove:(e,s,t=r[e],o=t?.indexOf(s)??-1)=>(~o&&t?.splice(o,1),a())}[s])});return c};
|
package/connect.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=(e,s={})=>{let o,t=[],n=0,a={},r=()=>(o||(o=new WebSocket(
|
|
1
|
+
const e=(e,s={})=>{let o,t=[],n=0,a={},r=()=>(o||(o=new WebSocket(/^(wss?):/.test(e)?e:`wss://ittysockets.io/c/${e}?`+new URLSearchParams(s)),o.onopen=()=>{for(;t.length;)o?.send(t.shift());for(let e of a.open??[])e();n&&o?.close()},o.onmessage=(e,s=JSON.parse(e.data))=>{for(let e of a[s.type??"message"]??[])e({...s,date:new Date(s.date)})},o.onclose=()=>{n=0,o=null;for(let e of a.close??[])e()}),l);const l=new Proxy(r,{get:(e,s)=>({open:r,close:()=>(1==o?.readyState?o.close():n=1,l),send:(e,s)=>(e=JSON.stringify(e),e=s?`@@${s}@@${e}`:e,1==o?.readyState?(o.send(e),l):(t.push(e),r())),push:(e,s)=>(n=1,l.send(e,s)),on:(e,s)=>((a[e]??=[]).push(s),r()),remove:(e,s,o=a[e],t=o?.indexOf(s)??-1)=>(~t&&o?.splice(t,1),r())}[s])});return l};export{e as connect};
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";exports.connect=(e,s={})=>{let o
|
|
1
|
+
"use strict";exports.connect=(e,s={})=>{let t,o=[],n=0,r={},a=()=>(t||(t=new WebSocket(/^(wss?):/.test(e)?e:`wss://ittysockets.io/c/${e}?`+new URLSearchParams(s)),t.onopen=()=>{for(;o.length;)t?.send(o.shift());for(let e of r.open??[])e();n&&t?.close()},t.onmessage=(e,s=JSON.parse(e.data))=>{for(let e of r[s.type??"message"]??[])e({...s,date:new Date(s.date)})},t.onclose=()=>{n=0,t=null;for(let e of r.close??[])e()}),c);const c=new Proxy(a,{get:(e,s)=>({open:a,close:()=>(1==t?.readyState?t.close():n=1,c),send:(e,s)=>(e=JSON.stringify(e),e=s?`@@${s}@@${e}`:e,1==t?.readyState?(t.send(e),c):(o.push(e),a())),push:(e,s)=>(n=1,c.send(e,s)),on:(e,s)=>((r[e]??=[]).push(s),a()),remove:(e,s,t=r[e],o=t?.indexOf(s)??-1)=>(~o&&t?.splice(o,1),a())}[s])});return c};
|
package/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=(e,s={})=>{let o,t=[],n=0,a={},r=()=>(o||(o=new WebSocket(
|
|
1
|
+
const e=(e,s={})=>{let o,t=[],n=0,a={},r=()=>(o||(o=new WebSocket(/^(wss?):/.test(e)?e:`wss://ittysockets.io/c/${e}?`+new URLSearchParams(s)),o.onopen=()=>{for(;t.length;)o?.send(t.shift());for(let e of a.open??[])e();n&&o?.close()},o.onmessage=(e,s=JSON.parse(e.data))=>{for(let e of a[s.type??"message"]??[])e({...s,date:new Date(s.date)})},o.onclose=()=>{n=0,o=null;for(let e of a.close??[])e()}),l);const l=new Proxy(r,{get:(e,s)=>({open:r,close:()=>(1==o?.readyState?o.close():n=1,l),send:(e,s)=>(e=JSON.stringify(e),e=s?`@@${s}@@${e}`:e,1==o?.readyState?(o.send(e),l):(t.push(e),r())),push:(e,s)=>(n=1,l.send(e,s)),on:(e,s)=>((a[e]??=[]).push(s),r()),remove:(e,s,o=a[e],t=o?.indexOf(s)??-1)=>(~t&&o?.splice(t,1),r())}[s])});return l};export{e as connect};
|