UNPKG

5.04 kBMarkdownView Raw
1# whisper
2
3Inspired by koa.js, follow koa, great project.
4
5TCP 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
16yarn add whisper
17```
18
19## Usage
20
21```js
22import Whisper from "whisper";
23
24const app = new Whisper();
25
26app.listen(3456);
27
28app.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
42app.use(async (ctx, next) => {
43 await next();
44 console.log(ctx.start);
45});
46```
47
48逻辑在 中间件中写,通过 app.use 调用,整体采用和 KOA 一样的洋葱模型。
49
50TODO: 补更多的 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
70The amount of received bytes.
71
72### ctx.bytesWritten
73
74`socket.bytesWritten`
75
76The amount of bytes sent.
77
78### ctx.localAddress
79
80`socket.localAddress`
81
82The string representation of the local IP address the remote client is connecting on.
83For example, in a server listening on '0.0.0.0', if a client connects on '192.168.1.1',
84the value of socket.localAddress would be '192.168.1.1'.
85
86### ctx.localPort
87
88`socket.localPort`
89
90The numeric representation of the local port. For example, 80 or 21.
91
92### ctx.remoteAddress
93
94`socket.remoteAddress`
95
96The string representation of the remote IP address. For example, '74.125.127.100' or '2001:4860:a005::68'.
97Value may be undefined if the socket is destroyed (for example, if the client disconnected).
98
99### ctx.remoteFamily
100
101`socket.remoteFamily`
102
103The string representation of the remote IP family. 'IPv4' or 'IPv6'.
104
105### ctx.remotePort
106
107`socket.remotePort`
108
109The 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
122Returns: <net.Socket> The socket itself.
123
124Pauses 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
130Returns: <net.Socket> The socket itself.
131
132Resumes reading after a call to socket.pause().
133
134### ctx.setTimeout(timeout[, callback])
135
136`socket.setTimeout(timeout[, callback])`
137
138Returns: <net.Socket> The socket itself.
139
140Sets the socket to timeout after timeout milliseconds of inactivity on the socket.
141By default net.Socket do not have a timeout.
142
143When an idle timeout is triggered the socket will receive a 'timeout'
144event but the connection will not be severed.
145The user must manually call socket.end() or socket.destroy() to end the connection.
146
147```js
148socket.setTimeout(3000);
149socket.on("timeout", () => {
150 console.log("socket timeout");
151 socket.end();
152});
153```
154
155If timeout is 0, then the existing idle timeout is disabled.
156
157The 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
164data <string> | <Buffer> | <Uint8Array>
165encoding <string> Only used when data is string. Default: utf8.
166callback <Function>
167Returns: <boolean>
168```
169
170Sends data on the socket. The second parameter specifies the encoding in the case of a string
171
172it defaults to UTF8 encoding.
173
174Returns true if the entire data was flushed successfully to the kernel buffer.
175Returns 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
178The optional callback parameter will be executed when the data is finally written out -
179this may not be immediately.
180
181See Writable stream write() method for more information.
182
183## Contributing
184
1851. Fork it!
1862. Create your feature branch: `git checkout -b my-new-feature`
1873. Commit your changes: `git commit -am 'Add some feature'`
1884. Push to the branch: `git push origin my-new-feature`
1895. Submit a pull request :D
190
191## Author
192
193**whisper** © [36node](https://github.com/36node), Released under the [MIT](./LICENSE) License.
194
195Authored 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)