Skip to content

Commit

Permalink
UDP模块翻译50%
Browse files Browse the repository at this point in the history
  • Loading branch information
Equim-chan committed Mar 13, 2017
1 parent 9879c3f commit 5fd60c0
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 39 deletions.
7 changes: 2 additions & 5 deletions dgram/class_dgram_socket.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
added: v0.1.99
-->

The `dgram.Socket` object is an [`EventEmitter`][] that encapsulates the
datagram functionality.

New instances of `dgram.Socket` are created using [`dgram.createSocket()`][].
The `new` keyword is not to be used to create `dgram.Socket` instances.
`dgram.Socket`对象是一个封装了数据包函数功能的[`EventEmitter`][]

`dgram.Socket`实例是由[`dgram.createSocket()`][]创建的。创建`dgram.Socket`实例不需要使用`new`关键字。
1 change: 1 addition & 0 deletions dgram/event_close.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ added: v0.1.99

The `'close'` event is emitted after a socket is closed with [`close()`][].
Once triggered, no new `'message'` events will be emitted on this socket.
`'close'`事件将在使用[`close()`][]关闭一个 socket 之后触发。该事件一旦触发,这个 socket 上将不会触发新的`'message'`事件。

3 changes: 1 addition & 2 deletions dgram/event_error.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ added: v0.1.99

* `exception` {Error}

The `'error'` event is emitted whenever any error occurs. The event handler
function is passed a single Error object.
当有任何错误发生时,`'error'`事件将被触发。事件发生时,回掉函数仅会接收到一个 Error 参数。

3 changes: 1 addition & 2 deletions dgram/event_listening.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
added: v0.1.99
-->

The `'listening'` event is emitted whenever a socket begins listening for
datagram messages. This occurs as soon as UDP sockets are created.
当一个 socket 开始监听数据包信息时,`'listening'`事件将被触发。该事件会在创建 UDP socket 之后被立即触发。

11 changes: 4 additions & 7 deletions dgram/event_message.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
added: v0.1.99
-->

* `msg` {Buffer} - The message
* `rinfo` {Object} - Remote address information
* `msg` {Buffer} - 消息
* `rinfo` {Object} - 远程地址信息

The `'message'` event is emitted when a new datagram is available on a socket.
The event handler function is passed two arguments: `msg` and `rinfo`. The
`msg` argument is a [`Buffer`][] and `rinfo` is an object with the sender's
address information provided by the `address`, `family` and `port` properties:
当有新的数据包被 socket 接收时,`'message'`事件会被触发。`msg``rinfo`会作为参数传递到该事件的处理函数中。`msg`参数是一个 [`Buffer`][]`rinfo`参数是一个提供了发送者地址信息的对象,包含`address``family``port`属性:

```js
socket.on('message', (msg, rinfo) => {
console.log('Received %d bytes from %s:%d\n',
console.log('收到来自 %s:%d 的 %d bytes 数据\n',
msg.length, rinfo.address, rinfo.port);
});
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ added: v0.6.9
-->

* `multicastAddress` {String}
* `multicastInterface` {String}, Optional
* `multicastInterface` {String}, 可选的

Tells the kernel to join a multicast group at the given `multicastAddress` and
`multicastInterface` using the `IP_ADD_MEMBERSHIP` socket option. If the
`multicastInterface` argument is not specified, the operating system will choose
one interface and will add membership to it. To add membership to every
available interface, call `addMembership` multiple times, once per interface.
通知内核将`multicastAddress``multicastInterface`提供的多路传送集合通过`IP_ADD_MEMBERSHIP`这个 socket 选项结合起来。若`multicastInterface`参数未指定,操作系统将会选择一个接口并向其添加成员。要为所有可用的接口添加成员,可以在每个接口上调用一次`addMembership`方法。

4 changes: 1 addition & 3 deletions dgram/socket_address.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@
added: v0.1.99
-->

Returns an object containing the address information for a socket.
For UDP sockets, this object will contain `address`, `family` and `port`
properties.
返回一个包含 socket 地址信息的对象。对于 UDP socket,该对象将包含`address``family``port`属性。

20 changes: 12 additions & 8 deletions dgram/socket_bind_port_address_callback.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,53 @@
added: v0.1.99
-->

* `port` {Number} - Integer, Optional
* `address` {String}, Optional
* `callback` {Function} with no parameters, Optional. Called when
binding is complete.
* `port` {Number} - 整数,可选的
* `address` {String}, 可选的
* `callback` {Function} (没有参数),可选的。当绑定完成时会被调用。

For UDP sockets, causes the `dgram.Socket` to listen for datagram messages on a
named `port` and optional `address`. If `port` is not specified, the operating
system will attempt to bind to a random port. If `address` is not specified,
the operating system will attempt to listen on all addresses. Once binding is
complete, a `'listening'` event is emitted and the optional `callback` function
is called.
对于 UDP socket,该方法会令`dgram.Socket`在指定的`port`和可选的`address`上监听数据包信息。若`port`未指定,操作系统会尝试绑定一个随机的端口。若`address`未指定,操作系统会尝试在所有地址上监听。绑定完成时会触发一个`'listening'`事件,并会调用`callback`方法。

Note that specifying both a `'listening'` event listener and passing a
`callback` to the `socket.bind()` method is not harmful but not very
useful.
注意,同时监听`'listening'`事件和在`socket.bind()`方法中传入`callback`参数并不会带来坏处,但也不是很有用。

A bound datagram socket keeps the Node.js process running to receive
datagram messages.
一个被绑定的数据包 socket 会令 Node.js 进程保持运行以接收数据包信息。

If binding fails, an `'error'` event is generated. In rare case (e.g.
attempting to bind with a closed socket), an [`Error`][] may be thrown.
若绑定失败,一个`'error'`事件会被触发。在极少数的情况下(例如尝试绑定一个已关闭的 socket),一个 [`Error`][] 会被抛出。

Example of a UDP server listening on port 41234:
一个监听 41234 端口的 UDP 服务器的例子:

```js
const dgram = require('dgram');
const server = dgram.createSocket('udp4');
server.on('error', (err) => {
console.log(`server error:\n${err.stack}`);
console.log(`服务器异常:\n${err.stack}`);
server.close();
});
server.on('message', (msg, rinfo) => {
console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
console.log(`服务器收到:${msg} 来自 ${rinfo.address}:${rinfo.port}`);
});
server.on('listening', () => {
var address = server.address();
console.log(`server listening ${address.address}:${address.port}`);
console.log(`服务器监听 ${address.address}:${address.port}`);
});
server.bind(41234);
// server listening 0.0.0.0:41234
// 服务器监听 0.0.0.0:41234
```

12 changes: 6 additions & 6 deletions dgram/udp_datagram_sockets.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@

> Stability: 2 - Stable
> 稳定性: 2 - 稳定的

<!-- name=dgram -->

The `dgram` module provides an implementation of UDP Datagram sockets.
`dgram`模块提供了 UDP 数据包 socket 的实现。

```js
const dgram = require('dgram');
const server = dgram.createSocket('udp4');
server.on('error', (err) => {
console.log(`server error:\n${err.stack}`);
console.log(`服务器异常:\n${err.stack}`);
server.close();
});
server.on('message', (msg, rinfo) => {
console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
console.log(`服务器收到:${msg} 来自 ${rinfo.address}:${rinfo.port}`);
});
server.on('listening', () => {
var address = server.address();
console.log(`server listening ${address.address}:${address.port}`);
console.log(`服务器监听 ${address.address}:${address.port}`);
});
server.bind(41234);
// server listening 0.0.0.0:41234
// 服务器监听 0.0.0.0:41234
```

0 comments on commit 5fd60c0

Please sign in to comment.