diff options
author | Guy Bedford <guybedford@gmail.com> | 2021-06-15 20:09:29 +0300 |
---|---|---|
committer | Guy Bedford <guybedford@gmail.com> | 2021-06-25 21:26:49 +0300 |
commit | f4d0a6a07bf72c556c5ba8e98b3b23327c273e80 (patch) | |
tree | bbc9c36d93f4868119d9d2a9bb776e003919d752 /doc/api/dgram.md | |
parent | 44ecd418923310aafbef8c6ccf9a9920bcf7af85 (diff) |
doc: esm examples /w imports for process, Buffer
PR-URL: https://github.com/nodejs/node/pull/39043
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Diffstat (limited to 'doc/api/dgram.md')
-rw-r--r-- | doc/api/dgram.md | 116 |
1 files changed, 110 insertions, 6 deletions
diff --git a/doc/api/dgram.md b/doc/api/dgram.md index 8813f9bffaf..365c30c9ae6 100644 --- a/doc/api/dgram.md +++ b/doc/api/dgram.md @@ -10,7 +10,30 @@ The `dgram` module provides an implementation of UDP datagram sockets. -```js +```mjs +import dgram from 'dgram'; + +const server = dgram.createSocket('udp4'); + +server.on('error', (err) => { + console.log(`server error:\n${err.stack}`); + server.close(); +}); + +server.on('message', (msg, rinfo) => { + console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`); +}); + +server.on('listening', () => { + const address = server.address(); + console.log(`server listening ${address.address}:${address.port}`); +}); + +server.bind(41234); +// Prints: server listening 0.0.0.0:41234 +``` + +```cjs const dgram = require('dgram'); const server = dgram.createSocket('udp4'); @@ -123,9 +146,25 @@ When sharing a UDP socket across multiple `cluster` workers, the `socket.addMembership()` function must be called only once or an `EADDRINUSE` error will occur: -```js +```mjs +import cluster from 'cluster'; +import dgram from 'dgram'; + +if (cluster.isPrimary) { + cluster.fork(); // Works ok. + cluster.fork(); // Fails with EADDRINUSE. +} else { + const s = dgram.createSocket('udp4'); + s.bind(1234, () => { + s.addMembership('224.0.0.114'); + }); +} +``` + +```cjs const cluster = require('cluster'); const dgram = require('dgram'); + if (cluster.isPrimary) { cluster.fork(); // Works ok. cluster.fork(); // Fails with EADDRINUSE. @@ -205,7 +244,30 @@ attempting to bind with a closed socket), an [`Error`][] may be thrown. Example of a UDP server listening on port 41234: -```js +```mjs +import dgram from 'dgram'; + +const server = dgram.createSocket('udp4'); + +server.on('error', (err) => { + console.log(`server error:\n${err.stack}`); + server.close(); +}); + +server.on('message', (msg, rinfo) => { + console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`); +}); + +server.on('listening', () => { + const address = server.address(); + console.log(`server listening ${address.address}:${address.port}`); +}); + +server.bind(41234); +// Prints: server listening 0.0.0.0:41234 +``` + +```cjs const dgram = require('dgram'); const server = dgram.createSocket('udp4'); @@ -480,8 +542,21 @@ This method throws [`ERR_SOCKET_BAD_PORT`][] if called on an unbound socket. Example of sending a UDP packet to a port on `localhost`; -```js +```mjs +import dgram from 'dgram'; +import { Buffer } from 'buffer'; + +const message = Buffer.from('Some bytes'); +const client = dgram.createSocket('udp4'); +client.send(message, 41234, 'localhost', (err) => { + client.close(); +}); +``` + +```cjs const dgram = require('dgram'); +const { Buffer } = require('buffer'); + const message = Buffer.from('Some bytes'); const client = dgram.createSocket('udp4'); client.send(message, 41234, 'localhost', (err) => { @@ -492,8 +567,22 @@ client.send(message, 41234, 'localhost', (err) => { Example of sending a UDP packet composed of multiple buffers to a port on `127.0.0.1`; -```js +```mjs +import dgram from 'dgram'; +import { Buffer } from 'buffer'; + +const buf1 = Buffer.from('Some '); +const buf2 = Buffer.from('bytes'); +const client = dgram.createSocket('udp4'); +client.send([buf1, buf2], 41234, (err) => { + client.close(); +}); +``` + +```cjs const dgram = require('dgram'); +const { Buffer } = require('buffer'); + const buf1 = Buffer.from('Some '); const buf2 = Buffer.from('bytes'); const client = dgram.createSocket('udp4'); @@ -510,8 +599,23 @@ however, sending multiple buffers is faster. Example of sending a UDP packet using a socket connected to a port on `localhost`: -```js +```mjs +import dgram from 'dgram'; +import { Buffer } from 'buffer'; + +const message = Buffer.from('Some bytes'); +const client = dgram.createSocket('udp4'); +client.connect(41234, 'localhost', (err) => { + client.send(message, (err) => { + client.close(); + }); +}); +``` + +```cjs const dgram = require('dgram'); +const { Buffer } = require('buffer'); + const message = Buffer.from('Some bytes'); const client = dgram.createSocket('udp4'); client.connect(41234, 'localhost', (err) => { |