heyujie
2021-05-24 4885600ecc369aa2e30a65de8dd7a410f13c34df
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
import * as WebSocket from 'ws';
import * as WebSocketStream from './';
 
{
    let ws = new WebSocket('ws://www.host.com/path');
    const stream = WebSocketStream(ws);
    ws = stream.socket;
 
    stream.setEncoding("utf8");
    stream.write("hello world");
 
    const message = stream.read(10);
    const message2 = stream.read();
}
 
{
    const stream = WebSocketStream('ws://www.host.com/path');
}
 
{
    // stream - url target with subprotocol
    const stream = WebSocketStream('ws://www.host.com/path', 'appProtocol-v1');
}
 
{
    // stream - url target with subprotocols, no options
    const stream = WebSocketStream('ws://www.host.com/path', ['appProtocol-v1', 'appProtocol-v2']);
}
 
{
    // stream - url target with options, no subprotocols
    const stream = WebSocketStream('ws://www.host.com/path', { maxPayload: 1024 });
}
 
{
    // stream - url target with subprotocol and options
    const stream = WebSocketStream(
        'ws://www.host.com/path',
        ['appProtocol-v1', 'appProtocol-v2'],
        { maxPayload: 1024 },
    );
}
 
{
    // stream - url target with subprotocols and options
    const stream = WebSocketStream(
        'ws://www.host.com/path',
        ['appProtocol-v1', 'appProtocol-v2'],
        { maxPayload: 1024 },
    );
}
 
{
    // dot server
    const wss = new WebSocketStream.Server({port: 8081});
    wss.on('stream', (stream, req) => {
      stream.write(stream.read());
      stream.end();
    });
}
 
{
    // dot createServer
    const wss = WebSocketStream.createServer({port: 8081});
    wss.on('stream', (stream, req) => {
      stream.write(stream.read());
      stream.end(); // closes underlying socket
    });
}