|
| 1 | +ZJS API for UDP datagram sockets |
| 2 | +================================ |
| 3 | + |
| 4 | +* [Introduction](#introduction) |
| 5 | +* [Web IDL](#web-idl) |
| 6 | +* [API Documentation](#api-documentation) |
| 7 | +* [Client Requirements](#requirements) |
| 8 | +* [Sample Apps](#sample-apps) |
| 9 | + |
| 10 | +Introduction |
| 11 | +------------ |
| 12 | +The `dgram` API is based on a subset of the |
| 13 | +[corresponding module](https://nodejs.org/api/dgram.html) in Node.js. |
| 14 | +It allows you to send and receive UDP datagrams. |
| 15 | + |
| 16 | +Web IDL |
| 17 | +------- |
| 18 | +This IDL provides an overview of the interface; see below for documentation of |
| 19 | +specific API functions. |
| 20 | + |
| 21 | +```javascript |
| 22 | +// require returns a socket factory object |
| 23 | +// var dgram = require('dgram'); |
| 24 | + |
| 25 | +[NoInterfaceObject] |
| 26 | +interface dgram { |
| 27 | + DgramSocket createSocket(string udp4_or_udp6); |
| 28 | +}; |
| 29 | + |
| 30 | +[NoInterfaceObject] |
| 31 | +interface DgramSocket { |
| 32 | + void on(string event, RecvCallback cb); |
| 33 | + void bind(int port, string ip_addr); |
| 34 | + void send(Buffer buf, unsigned long offset, unsigned long len, int port, string ip_addr, [SendCallback cb]); |
| 35 | + void close(); |
| 36 | +}; |
| 37 | + |
| 38 | +callback RecvCallback = void (Buffer msg, RemoteInfo rinfo); |
| 39 | +callback SendCallback = void (Error err); // or undefined if no error |
| 40 | + |
| 41 | + |
| 42 | +callback EventCallback = void (various); // callback arg depends on event |
| 43 | + |
| 44 | +dictionary RemoteInfo { |
| 45 | + string ip_addr; |
| 46 | + string family; |
| 47 | + int port; |
| 48 | +}; |
| 49 | +``` |
| 50 | + |
| 51 | +API Documentation |
| 52 | +----------------- |
| 53 | +### dgram.createSocket |
| 54 | + |
| 55 | +`DgramSocket createSocket(string type);` |
| 56 | + |
| 57 | +Create a datagram socket of given type, which must be `'udp4'` or `'udp6'`. |
| 58 | + |
| 59 | +### DgramSocket.on |
| 60 | + |
| 61 | +`void on(string event, RecvCallback callback);` |
| 62 | + |
| 63 | +Registers a callback. The `event` may be one of the following: |
| 64 | + |
| 65 | +* `'message'` - a datagram received. `callback` receives a Buffer |
| 66 | + containing incoming datagram data and RemoteInfo dictionary with |
| 67 | + information about the source address. |
| 68 | +* `'error'` - error occurred. `callback` receives an Error object. |
| 69 | + (In the current version, this callback is never called, but this |
| 70 | + will change in future versions.) |
| 71 | + |
| 72 | +### DgramSocket.bind |
| 73 | + |
| 74 | +`void bind(int port, string ip_addr);` |
| 75 | + |
| 76 | +Bind socket to a local address and port. This is required operation for |
| 77 | +server-side sockets, i.e. sockets which wait and receive data from other |
| 78 | +network nodes. `ip_addr` must be a string representing an IP address. |
| 79 | +This module does not support domain name resolution, so only IP addresses |
| 80 | +are allowed. An example of IPv4 address: `'192.0.2.1'`, IPV6: `'2001:db8::1'`. |
| 81 | + |
| 82 | +### DgramSocket.send |
| 83 | + |
| 84 | +`void send(Buffer buf, unsigned long offset, unsigned long len, int port, string ip_addr, [SendCallback cb]);` |
| 85 | + |
| 86 | +Send data contained in a buffer to remote network node. A subset of |
| 87 | +data in `buf` can be sent using `offset` and `len` parameters. To send |
| 88 | +entire buffer, using values `0` and `buf.length` respectively. See |
| 89 | +`bind()` method description for the format of `ip_addr`. An optional |
| 90 | +callback may be provided, which will be called with the result of the send |
| 91 | +operation: either NetworkError object in case of error, or `undefined` |
| 92 | +on success. |
| 93 | + |
| 94 | +### DgramSocket.close |
| 95 | + |
| 96 | +`void close();` |
| 97 | + |
| 98 | +Closes socket. |
| 99 | + |
| 100 | +Sample Apps |
| 101 | +----------- |
| 102 | +* [IPv4 UDP echo server](../samples/UDPEchoServ4.js) |
| 103 | +* [IPv6 UDP echo server with `offset` param to send()](../samples/UDPEchoServ6.js) |
0 commit comments