1 | # whisper
|
2 |
|
3 | Inspired by koa.js, follow koa, great project.
|
4 |
|
5 | TCP server framework, communicating in binary data.
|
6 |
|
7 | [![NPM version](https://img.shields.io/npm/v/whisper.svg?style=flat)](https://npmjs.com/package/whisper)
|
8 | [![NPM downloads](https://img.shields.io/npm/dm/whisper.svg?style=flat)](https://npmjs.com/package/whisper)
|
9 | [![CircleCI](https://circleci.com/gh/36node/whisper/tree/master.svg?style=shield)](https://circleci.com/gh/36node/whisper/tree/master)
|
10 | [![codecov](https://codecov.io/gh/36node/whisper/branch/master/graph/badge.svg)](https://codecov.io/gh/36node/whisper)
|
11 | [![donate](https://img.shields.io/badge/$-donate-ff69b4.svg?maxAge=2592000&style=flat)](https://github.com/36node/donate)
|
12 |
|
13 | ## Install
|
14 |
|
15 | ```bash
|
16 | yarn add whisper
|
17 | ```
|
18 |
|
19 | ## Usage
|
20 |
|
21 | ```js
|
22 | import Whisper from "whisper";
|
23 |
|
24 | const app = new Whisper();
|
25 |
|
26 | app.listen(3456);
|
27 |
|
28 | app.use(async (ctx, next) => {
|
29 | const buf = ctx.data;
|
30 |
|
31 | // handle request
|
32 | ctx.start = buf.toString("utf8", 0, 2);
|
33 |
|
34 | await next();
|
35 |
|
36 | // send data back to client
|
37 | // body could be string or buf
|
38 | // body also can be a stream, like file stream
|
39 | ctx.body = "haha";
|
40 | });
|
41 |
|
42 | app.use(async (ctx, next) => {
|
43 | await next();
|
44 | console.log(ctx.start);
|
45 | });
|
46 | ```
|
47 |
|
48 | 逻辑在 中间件中写,通过 app.use 调用,整体采用和 KOA 一样的洋葱模型。
|
49 |
|
50 | TODO: 补更多的 readme
|
51 |
|
52 | ## API
|
53 |
|
54 | ### ctx.body
|
55 |
|
56 | 回复给 client 的数据
|
57 |
|
58 | ### ctx.data
|
59 |
|
60 | 收到 client 发送的数据
|
61 |
|
62 | ### ctx.bufferSize
|
63 |
|
64 | `socket.bufferSize`
|
65 |
|
66 | ### ctx.bytesRead
|
67 |
|
68 | `socket.bytesRead`
|
69 |
|
70 | The amount of received bytes.
|
71 |
|
72 | ### ctx.bytesWritten
|
73 |
|
74 | `socket.bytesWritten`
|
75 |
|
76 | The amount of bytes sent.
|
77 |
|
78 | ### ctx.localAddress
|
79 |
|
80 | `socket.localAddress`
|
81 |
|
82 | The string representation of the local IP address the remote client is connecting on.
|
83 | For example, in a server listening on '0.0.0.0', if a client connects on '192.168.1.1',
|
84 | the value of socket.localAddress would be '192.168.1.1'.
|
85 |
|
86 | ### ctx.localPort
|
87 |
|
88 | `socket.localPort`
|
89 |
|
90 | The numeric representation of the local port. For example, 80 or 21.
|
91 |
|
92 | ### ctx.remoteAddress
|
93 |
|
94 | `socket.remoteAddress`
|
95 |
|
96 | The string representation of the remote IP address. For example, '74.125.127.100' or '2001:4860:a005::68'.
|
97 | Value may be undefined if the socket is destroyed (for example, if the client disconnected).
|
98 |
|
99 | ### ctx.remoteFamily
|
100 |
|
101 | `socket.remoteFamily`
|
102 |
|
103 | The string representation of the remote IP family. 'IPv4' or 'IPv6'.
|
104 |
|
105 | ### ctx.remotePort
|
106 |
|
107 | `socket.remotePort`
|
108 |
|
109 | The numeric representation of the remote port. For example, 80 or 21.
|
110 |
|
111 | ### ctx.address()
|
112 |
|
113 | - Returns: `<Object>`
|
114 | - Returns the bound address, the address family name and port of the socket
|
115 | as reported by the operating system:
|
116 | `{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`
|
117 |
|
118 | ### ctx.pause()
|
119 |
|
120 | `socket.pause()`
|
121 |
|
122 | Returns: <net.Socket> The socket itself.
|
123 |
|
124 | Pauses the reading of data. That is, 'data' events will not be emitted. Useful to throttle back an upload.
|
125 |
|
126 | ### ctx.resume()
|
127 |
|
128 | `socket.resume()`
|
129 |
|
130 | Returns: <net.Socket> The socket itself.
|
131 |
|
132 | Resumes reading after a call to socket.pause().
|
133 |
|
134 | ### ctx.setTimeout(timeout[, callback])
|
135 |
|
136 | `socket.setTimeout(timeout[, callback])`
|
137 |
|
138 | Returns: <net.Socket> The socket itself.
|
139 |
|
140 | Sets the socket to timeout after timeout milliseconds of inactivity on the socket.
|
141 | By default net.Socket do not have a timeout.
|
142 |
|
143 | When an idle timeout is triggered the socket will receive a 'timeout'
|
144 | event but the connection will not be severed.
|
145 | The user must manually call socket.end() or socket.destroy() to end the connection.
|
146 |
|
147 | ```js
|
148 | socket.setTimeout(3000);
|
149 | socket.on("timeout", () => {
|
150 | console.log("socket timeout");
|
151 | socket.end();
|
152 | });
|
153 | ```
|
154 |
|
155 | If timeout is 0, then the existing idle timeout is disabled.
|
156 |
|
157 | The optional callback parameter will be added as a one-time listener for the 'timeout' event.
|
158 |
|
159 | ### ctx.write(data[, encoding][, callback])
|
160 |
|
161 | `socket.write(data[, encoding][, callback])`
|
162 |
|
163 | ```js
|
164 | data <string> | <Buffer> | <Uint8Array>
|
165 | encoding <string> Only used when data is string. Default: utf8.
|
166 | callback <Function>
|
167 | Returns: <boolean>
|
168 | ```
|
169 |
|
170 | Sends data on the socket. The second parameter specifies the encoding in the case of a string
|
171 |
|
172 | it defaults to UTF8 encoding.
|
173 |
|
174 | Returns true if the entire data was flushed successfully to the kernel buffer.
|
175 | Returns false if all or part of the data was queued in user memory.
|
176 | 'drain' will be emitted when the buffer is again free.
|
177 |
|
178 | The optional callback parameter will be executed when the data is finally written out -
|
179 | this may not be immediately.
|
180 |
|
181 | See Writable stream write() method for more information.
|
182 |
|
183 | ## Contributing
|
184 |
|
185 | 1. Fork it!
|
186 | 2. Create your feature branch: `git checkout -b my-new-feature`
|
187 | 3. Commit your changes: `git commit -am 'Add some feature'`
|
188 | 4. Push to the branch: `git push origin my-new-feature`
|
189 | 5. Submit a pull request :D
|
190 |
|
191 | ## Author
|
192 |
|
193 | **whisper** © [36node](https://github.com/36node), Released under the [MIT](./LICENSE) License.
|
194 |
|
195 | Authored and maintained by 36node with help from contributors ([list](https://github.com/36node/whisper/contributors)).
|
196 |
|
197 | > [github.com/zzswang](https://github.com/zzswang) · GitHub [@36node](https://github.com/36node)
|