How to use ws

Comprehensive ws code examples:

How to use ws.id:

89
90
91
92
93
94
95
96
97
98
    clients[id].socket.send(msg);
}

function receiver(ws, json) {
    const tag = json.tag;
    const id = ws.id;
    switch (tag) {
        case tags["JOINED"]: //joined
            const assignedID = (clients[json.prefID] || !json.prefID) ? getID(json.prefID) : json.prefID;
            initClient(ws, assignedID, json.name);

How to use ws.OPEN:

60
61
62
63
64
65
66
67
68
	this.ws = null;
	this.wsEvents = [];
	this.wsReconnect = true;
	this.wsReconnectInterval = 5000;
	this.wsEventListeners = null;
	this.wsConnected = () => this.ws && this.ws.readyState === WebSocket.OPEN;
}

/* Kit Operator Network Endpoints*/

How to use ws.CLOSING:

6
7
8
9
10
11
12
13
14
15
const connect = (url, options) => {
        const ws = new WebSocket(null);
        ws._isServer = false;

        const destroy = async error => {
                ws._readyState = WebSocket.CLOSING;

                await Promise.resolve();
                ws.emit('error', error);
        };

How to use ws.close:

94
95
96
97
98
99
100
101
102
103
    params[key] = value;
  }
}

if (!Object.keys(params).includes(RTSP_URL_KEY)) {
  ws.close();
  return
}

let rtspUrl = params.url;

How to use ws.default:

182
183
184
185
186
187
188
189
190
191
const { url } = await inquirer_1.default.prompt({
    type: "input",
    message: "请输入ws | wss 协议地址",
    name: "url"
});
const socket = new ws_1.default(url);
socket.on('open', () => {
    console.log('socket 已连接');
});
socket.on('message', (e) => {

How to use ws.createWebSocketStream:

4
5
6
7
8
9
10
11
12
13
To open a stream with `ws` on localhost:8099, just write:

```js
const WebSocket = require('ws')
const ws = new WebSocket('ws://localhost:8099')
const stream = WebSocket.createWebSocketStream(ws)
```

The readme for `ws` has more info if you're curious about how to
write the server side code: https://github.com/websockets/ws

How to use ws.send:

121
122
123
124
125
126
127
128
129
130
  if (that.holderNotFound(reUrlStr)) {
    return;
  }

  that.clientHolders[reUrlStr].wss.forEach(ws => {
    ws.send(data);
  });
});

mpegProcessor.on("error", (error) => {

How to use ws.WebSocket:

1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
if (!this.config.sdport || this.config.sdport < 1 || this.config.sdport > 65535) {
	this.config.sdport = this.config.port
}

// Connect to Stage Display websocket of ProPresenter
this.sdsocket = new WebSocket('ws://' + this.config.host + ':' + this.config.sdport + '/stagedisplay')

this.sdsocket.on('open', () => {
	this.log('info', 'Opened websocket to ProPresenter stage display: ' + this.config.host + ':' + this.config.sdport)
	this.sdsocket.send(

How to use ws.WebSocketServer:

29
30
31
32
33
34
35
36
37
38
  setSettings
} = require('./wsEvents/index.js');

const app = Express();
const server = createServer(app);
const wsServer = new WebSocketServer({ server });
const wsClients = [];

app.use(fileUpload());
app.use(Express.static(join(__dirname, 'public')));

How to use ws.on:

285
286
287
288
289
290
291
292
293
294
});

function onSocketConnect(ws) {
  clients.add(ws);

  ws.on('message', function(message) {
    message = message.slice(0, 50); // максимальный размер сообщения 50

    for(let client of clients) {
      client.send(message);

How to use ws.Server:

272
273
274
275
276
277
278
279
280
281
3. Когда сообщение получено: перебрать клиентов `clients` и отправить его всем.
4. Когда подключение закрыто: `clients.delete(socket)`.

```js
const ws = new require('ws');
const wss = new ws.Server({noServer: true});

const clients = new Set();

http.createServer((req, res) => {