版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. # ws: a Node.js WebSocket library
  2. [![Version npm](https://img.shields.io/npm/v/ws.svg?logo=npm)](https://www.npmjs.com/package/ws)
  3. [![CI](https://img.shields.io/github/actions/workflow/status/websockets/ws/ci.yml?branch=master&label=CI&logo=github)](https://github.com/websockets/ws/actions?query=workflow%3ACI+branch%3Amaster)
  4. [![Coverage Status](https://img.shields.io/coveralls/websockets/ws/master.svg?logo=coveralls)](https://coveralls.io/github/websockets/ws)
  5. ws is a simple to use, blazing fast, and thoroughly tested WebSocket client and
  6. server implementation.
  7. Passes the quite extensive Autobahn test suite: [server][server-report],
  8. [client][client-report].
  9. **Note**: This module does not work in the browser. The client in the docs is a
  10. reference to a back end with the role of a client in the WebSocket
  11. communication. Browser clients must use the native
  12. [`WebSocket`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket)
  13. object. To make the same code work seamlessly on Node.js and the browser, you
  14. can use one of the many wrappers available on npm, like
  15. [isomorphic-ws](https://github.com/heineiuo/isomorphic-ws).
  16. ## Table of Contents
  17. - [Protocol support](#protocol-support)
  18. - [Installing](#installing)
  19. - [Opt-in for performance](#opt-in-for-performance)
  20. - [API docs](#api-docs)
  21. - [WebSocket compression](#websocket-compression)
  22. - [Usage examples](#usage-examples)
  23. - [Sending and receiving text data](#sending-and-receiving-text-data)
  24. - [Sending binary data](#sending-binary-data)
  25. - [Simple server](#simple-server)
  26. - [External HTTP/S server](#external-https-server)
  27. - [Multiple servers sharing a single HTTP/S server](#multiple-servers-sharing-a-single-https-server)
  28. - [Client authentication](#client-authentication)
  29. - [Server broadcast](#server-broadcast)
  30. - [Round-trip time](#round-trip-time)
  31. - [Use the Node.js streams API](#use-the-nodejs-streams-api)
  32. - [Other examples](#other-examples)
  33. - [FAQ](#faq)
  34. - [How to get the IP address of the client?](#how-to-get-the-ip-address-of-the-client)
  35. - [How to detect and close broken connections?](#how-to-detect-and-close-broken-connections)
  36. - [How to connect via a proxy?](#how-to-connect-via-a-proxy)
  37. - [Changelog](#changelog)
  38. - [License](#license)
  39. ## Protocol support
  40. - **HyBi drafts 07-12** (Use the option `protocolVersion: 8`)
  41. - **HyBi drafts 13-17** (Current default, alternatively option
  42. `protocolVersion: 13`)
  43. ## Installing
  44. ```
  45. npm install ws
  46. ```
  47. ### Opt-in for performance
  48. There are 2 optional modules that can be installed along side with the ws
  49. module. These modules are binary addons that improve the performance of certain
  50. operations. Prebuilt binaries are available for the most popular platforms so
  51. you don't necessarily need to have a C++ compiler installed on your machine.
  52. - `npm install --save-optional bufferutil`: Allows to efficiently perform
  53. operations such as masking and unmasking the data payload of the WebSocket
  54. frames.
  55. - `npm install --save-optional utf-8-validate`: Allows to efficiently check if a
  56. message contains valid UTF-8.
  57. To not even try to require and use these modules, use the
  58. [`WS_NO_BUFFER_UTIL`](./doc/ws.md#ws_no_buffer_util) and
  59. [`WS_NO_UTF_8_VALIDATE`](./doc/ws.md#ws_no_utf_8_validate) environment
  60. variables. These might be useful to enhance security in systems where a user can
  61. put a package in the package search path of an application of another user, due
  62. to how the Node.js resolver algorithm works.
  63. The `utf-8-validate` module is not needed and is not required, even if it is
  64. already installed, regardless of the value of the `WS_NO_UTF_8_VALIDATE`
  65. environment variable, if [`buffer.isUtf8()`][] is available.
  66. ## API docs
  67. See [`/doc/ws.md`](./doc/ws.md) for Node.js-like documentation of ws classes and
  68. utility functions.
  69. ## WebSocket compression
  70. ws supports the [permessage-deflate extension][permessage-deflate] which enables
  71. the client and server to negotiate a compression algorithm and its parameters,
  72. and then selectively apply it to the data payloads of each WebSocket message.
  73. The extension is disabled by default on the server and enabled by default on the
  74. client. It adds a significant overhead in terms of performance and memory
  75. consumption so we suggest to enable it only if it is really needed.
  76. Note that Node.js has a variety of issues with high-performance compression,
  77. where increased concurrency, especially on Linux, can lead to [catastrophic
  78. memory fragmentation][node-zlib-bug] and slow performance. If you intend to use
  79. permessage-deflate in production, it is worthwhile to set up a test
  80. representative of your workload and ensure Node.js/zlib will handle it with
  81. acceptable performance and memory usage.
  82. Tuning of permessage-deflate can be done via the options defined below. You can
  83. also use `zlibDeflateOptions` and `zlibInflateOptions`, which is passed directly
  84. into the creation of [raw deflate/inflate streams][node-zlib-deflaterawdocs].
  85. See [the docs][ws-server-options] for more options.
  86. ```js
  87. import WebSocket, { WebSocketServer } from 'ws';
  88. const wss = new WebSocketServer({
  89. port: 8080,
  90. perMessageDeflate: {
  91. zlibDeflateOptions: {
  92. // See zlib defaults.
  93. chunkSize: 1024,
  94. memLevel: 7,
  95. level: 3
  96. },
  97. zlibInflateOptions: {
  98. chunkSize: 10 * 1024
  99. },
  100. // Other options settable:
  101. clientNoContextTakeover: true, // Defaults to negotiated value.
  102. serverNoContextTakeover: true, // Defaults to negotiated value.
  103. serverMaxWindowBits: 10, // Defaults to negotiated value.
  104. // Below options specified as default values.
  105. concurrencyLimit: 10, // Limits zlib concurrency for perf.
  106. threshold: 1024 // Size (in bytes) below which messages
  107. // should not be compressed if context takeover is disabled.
  108. }
  109. });
  110. ```
  111. The client will only use the extension if it is supported and enabled on the
  112. server. To always disable the extension on the client set the
  113. `perMessageDeflate` option to `false`.
  114. ```js
  115. import WebSocket from 'ws';
  116. const ws = new WebSocket('ws://www.host.com/path', {
  117. perMessageDeflate: false
  118. });
  119. ```
  120. ## Usage examples
  121. ### Sending and receiving text data
  122. ```js
  123. import WebSocket from 'ws';
  124. const ws = new WebSocket('ws://www.host.com/path');
  125. ws.on('error', console.error);
  126. ws.on('open', function open() {
  127. ws.send('something');
  128. });
  129. ws.on('message', function message(data) {
  130. console.log('received: %s', data);
  131. });
  132. ```
  133. ### Sending binary data
  134. ```js
  135. import WebSocket from 'ws';
  136. const ws = new WebSocket('ws://www.host.com/path');
  137. ws.on('error', console.error);
  138. ws.on('open', function open() {
  139. const array = new Float32Array(5);
  140. for (var i = 0; i < array.length; ++i) {
  141. array[i] = i / 2;
  142. }
  143. ws.send(array);
  144. });
  145. ```
  146. ### Simple server
  147. ```js
  148. import { WebSocketServer } from 'ws';
  149. const wss = new WebSocketServer({ port: 8080 });
  150. wss.on('connection', function connection(ws) {
  151. ws.on('error', console.error);
  152. ws.on('message', function message(data) {
  153. console.log('received: %s', data);
  154. });
  155. ws.send('something');
  156. });
  157. ```
  158. ### External HTTP/S server
  159. ```js
  160. import { createServer } from 'https';
  161. import { readFileSync } from 'fs';
  162. import { WebSocketServer } from 'ws';
  163. const server = createServer({
  164. cert: readFileSync('/path/to/cert.pem'),
  165. key: readFileSync('/path/to/key.pem')
  166. });
  167. const wss = new WebSocketServer({ server });
  168. wss.on('connection', function connection(ws) {
  169. ws.on('error', console.error);
  170. ws.on('message', function message(data) {
  171. console.log('received: %s', data);
  172. });
  173. ws.send('something');
  174. });
  175. server.listen(8080);
  176. ```
  177. ### Multiple servers sharing a single HTTP/S server
  178. ```js
  179. import { createServer } from 'http';
  180. import { parse } from 'url';
  181. import { WebSocketServer } from 'ws';
  182. const server = createServer();
  183. const wss1 = new WebSocketServer({ noServer: true });
  184. const wss2 = new WebSocketServer({ noServer: true });
  185. wss1.on('connection', function connection(ws) {
  186. ws.on('error', console.error);
  187. // ...
  188. });
  189. wss2.on('connection', function connection(ws) {
  190. ws.on('error', console.error);
  191. // ...
  192. });
  193. server.on('upgrade', function upgrade(request, socket, head) {
  194. const { pathname } = parse(request.url);
  195. if (pathname === '/foo') {
  196. wss1.handleUpgrade(request, socket, head, function done(ws) {
  197. wss1.emit('connection', ws, request);
  198. });
  199. } else if (pathname === '/bar') {
  200. wss2.handleUpgrade(request, socket, head, function done(ws) {
  201. wss2.emit('connection', ws, request);
  202. });
  203. } else {
  204. socket.destroy();
  205. }
  206. });
  207. server.listen(8080);
  208. ```
  209. ### Client authentication
  210. ```js
  211. import { createServer } from 'http';
  212. import { WebSocketServer } from 'ws';
  213. function onSocketError(err) {
  214. console.error(err);
  215. }
  216. const server = createServer();
  217. const wss = new WebSocketServer({ noServer: true });
  218. wss.on('connection', function connection(ws, request, client) {
  219. ws.on('error', console.error);
  220. ws.on('message', function message(data) {
  221. console.log(`Received message ${data} from user ${client}`);
  222. });
  223. });
  224. server.on('upgrade', function upgrade(request, socket, head) {
  225. socket.on('error', onSocketError);
  226. // This function is not defined on purpose. Implement it with your own logic.
  227. authenticate(request, function next(err, client) {
  228. if (err || !client) {
  229. socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
  230. socket.destroy();
  231. return;
  232. }
  233. socket.removeListener('error', onSocketError);
  234. wss.handleUpgrade(request, socket, head, function done(ws) {
  235. wss.emit('connection', ws, request, client);
  236. });
  237. });
  238. });
  239. server.listen(8080);
  240. ```
  241. Also see the provided [example][session-parse-example] using `express-session`.
  242. ### Server broadcast
  243. A client WebSocket broadcasting to all connected WebSocket clients, including
  244. itself.
  245. ```js
  246. import WebSocket, { WebSocketServer } from 'ws';
  247. const wss = new WebSocketServer({ port: 8080 });
  248. wss.on('connection', function connection(ws) {
  249. ws.on('error', console.error);
  250. ws.on('message', function message(data, isBinary) {
  251. wss.clients.forEach(function each(client) {
  252. if (client.readyState === WebSocket.OPEN) {
  253. client.send(data, { binary: isBinary });
  254. }
  255. });
  256. });
  257. });
  258. ```
  259. A client WebSocket broadcasting to every other connected WebSocket clients,
  260. excluding itself.
  261. ```js
  262. import WebSocket, { WebSocketServer } from 'ws';
  263. const wss = new WebSocketServer({ port: 8080 });
  264. wss.on('connection', function connection(ws) {
  265. ws.on('error', console.error);
  266. ws.on('message', function message(data, isBinary) {
  267. wss.clients.forEach(function each(client) {
  268. if (client !== ws && client.readyState === WebSocket.OPEN) {
  269. client.send(data, { binary: isBinary });
  270. }
  271. });
  272. });
  273. });
  274. ```
  275. ### Round-trip time
  276. ```js
  277. import WebSocket from 'ws';
  278. const ws = new WebSocket('wss://websocket-echo.com/');
  279. ws.on('error', console.error);
  280. ws.on('open', function open() {
  281. console.log('connected');
  282. ws.send(Date.now());
  283. });
  284. ws.on('close', function close() {
  285. console.log('disconnected');
  286. });
  287. ws.on('message', function message(data) {
  288. console.log(`Round-trip time: ${Date.now() - data} ms`);
  289. setTimeout(function timeout() {
  290. ws.send(Date.now());
  291. }, 500);
  292. });
  293. ```
  294. ### Use the Node.js streams API
  295. ```js
  296. import WebSocket, { createWebSocketStream } from 'ws';
  297. const ws = new WebSocket('wss://websocket-echo.com/');
  298. const duplex = createWebSocketStream(ws, { encoding: 'utf8' });
  299. duplex.on('error', console.error);
  300. duplex.pipe(process.stdout);
  301. process.stdin.pipe(duplex);
  302. ```
  303. ### Other examples
  304. For a full example with a browser client communicating with a ws server, see the
  305. examples folder.
  306. Otherwise, see the test cases.
  307. ## FAQ
  308. ### How to get the IP address of the client?
  309. The remote IP address can be obtained from the raw socket.
  310. ```js
  311. import { WebSocketServer } from 'ws';
  312. const wss = new WebSocketServer({ port: 8080 });
  313. wss.on('connection', function connection(ws, req) {
  314. const ip = req.socket.remoteAddress;
  315. ws.on('error', console.error);
  316. });
  317. ```
  318. When the server runs behind a proxy like NGINX, the de-facto standard is to use
  319. the `X-Forwarded-For` header.
  320. ```js
  321. wss.on('connection', function connection(ws, req) {
  322. const ip = req.headers['x-forwarded-for'].split(',')[0].trim();
  323. ws.on('error', console.error);
  324. });
  325. ```
  326. ### How to detect and close broken connections?
  327. Sometimes the link between the server and the client can be interrupted in a way
  328. that keeps both the server and the client unaware of the broken state of the
  329. connection (e.g. when pulling the cord).
  330. In these cases ping messages can be used as a means to verify that the remote
  331. endpoint is still responsive.
  332. ```js
  333. import { WebSocketServer } from 'ws';
  334. function heartbeat() {
  335. this.isAlive = true;
  336. }
  337. const wss = new WebSocketServer({ port: 8080 });
  338. wss.on('connection', function connection(ws) {
  339. ws.isAlive = true;
  340. ws.on('error', console.error);
  341. ws.on('pong', heartbeat);
  342. });
  343. const interval = setInterval(function ping() {
  344. wss.clients.forEach(function each(ws) {
  345. if (ws.isAlive === false) return ws.terminate();
  346. ws.isAlive = false;
  347. ws.ping();
  348. });
  349. }, 30000);
  350. wss.on('close', function close() {
  351. clearInterval(interval);
  352. });
  353. ```
  354. Pong messages are automatically sent in response to ping messages as required by
  355. the spec.
  356. Just like the server example above your clients might as well lose connection
  357. without knowing it. You might want to add a ping listener on your clients to
  358. prevent that. A simple implementation would be:
  359. ```js
  360. import WebSocket from 'ws';
  361. function heartbeat() {
  362. clearTimeout(this.pingTimeout);
  363. // Use `WebSocket#terminate()`, which immediately destroys the connection,
  364. // instead of `WebSocket#close()`, which waits for the close timer.
  365. // Delay should be equal to the interval at which your server
  366. // sends out pings plus a conservative assumption of the latency.
  367. this.pingTimeout = setTimeout(() => {
  368. this.terminate();
  369. }, 30000 + 1000);
  370. }
  371. const client = new WebSocket('wss://websocket-echo.com/');
  372. client.on('error', console.error);
  373. client.on('open', heartbeat);
  374. client.on('ping', heartbeat);
  375. client.on('close', function clear() {
  376. clearTimeout(this.pingTimeout);
  377. });
  378. ```
  379. ### How to connect via a proxy?
  380. Use a custom `http.Agent` implementation like [https-proxy-agent][] or
  381. [socks-proxy-agent][].
  382. ## Changelog
  383. We're using the GitHub [releases][changelog] for changelog entries.
  384. ## License
  385. [MIT](LICENSE)
  386. [`buffer.isutf8()`]: https://nodejs.org/api/buffer.html#bufferisutf8input
  387. [changelog]: https://github.com/websockets/ws/releases
  388. [client-report]: http://websockets.github.io/ws/autobahn/clients/
  389. [https-proxy-agent]: https://github.com/TooTallNate/node-https-proxy-agent
  390. [node-zlib-bug]: https://github.com/nodejs/node/issues/8871
  391. [node-zlib-deflaterawdocs]:
  392. https://nodejs.org/api/zlib.html#zlib_zlib_createdeflateraw_options
  393. [permessage-deflate]: https://tools.ietf.org/html/rfc7692
  394. [server-report]: http://websockets.github.io/ws/autobahn/servers/
  395. [session-parse-example]: ./examples/express-session-parse
  396. [socks-proxy-agent]: https://github.com/TooTallNate/node-socks-proxy-agent
  397. [ws-server-options]: ./doc/ws.md#new-websocketserveroptions-callback