udp-inprism v0.0.8
udp-inprism
项目介绍
nodejs udp socket cluster
源代码来自:https://github.com/mafintosh/udp-request ;本库做部分修改。
软件架构
软件架构说明
from mafintosh/udp-request@1.5.0
使用说明
udp-inprism
基于udp-request@1.5.0基础上修改。增加了udpSend方法和修改一些事件逻辑判断 udpSend方法只要是解决向目标服务器发送原生的数据报。因为udp-request方法发送的数据包都添加了tid 字段占2位。
本类在原有的tid字段后再添加了 tag 字段占1位,用来判断收到的数据包是否为封装过的数据包。用于onmessage事件的判断。
npm install udp-inprism
Usage
var udp = require('udp-inprism')
var socket = udp()
socket.on('request', function (request, peer) {
console.log('request:', request.toString())
socket.response('echo: ' + request.toString(), peer)
})
socket.listen(10000, function () {
socket.request('hello', {port: 10000, host: '127.0.0.1'}, function (err, response) {
console.log('response', response.toString())
socket.destroy()
})
})
API
var socket = udp([options])
Create a new request/response udp socket. Options include:
. options <Object> 允许的选项是:
timeout: 1000, // request timeout
socket: udpSocket, // supply your own udp socket
retry: true, // retry requests if they time out. defaults to false
requestEncoding: someEncoder, // abstract-encoding compliant encoder
responseEncoding: someEncoder, // abstract-encoding compliant encoder
type <string> 套接字族. 必须是 'udp4' 或 'udp6'. 必需填.
reuseAddr <boolean> 若设置为 true socket.bind() ,则会 重用地址,即时另一个进程已经在其上面绑定了一个套接字。 默认是 false.
. 返回: <dgram.Socket>
. 创建一个 dgram.Socket 对象. 一旦创建了套接字,调用 socket.bind() 会指示套接字开始监听数据报消息。如果 address 和 port 没传给 socket.bind(), 那么这个方法会把这个套接字绑定到 "全部接口" 地址的一个随机端口(这适用于 udp4 和 udp6 套接字)。 绑定的地址和端口可以通过 socket.address().address 和socket.address().port 来获取。
var id = socket.request(buffer, peer, [options], [callback])
Send a new request. buffer
is the request payload and peer
should be an object containing {port, host}
.
When the response arrives (or the request times out) the callback is called with the following arguments
callback(error, response, peer)
Options include:
{
retry: true
}
socket.udpSend(buffer, peer, [options], [callback])
原生dgram 的send方法引用. 不进入udp-inprism计时内。
callback(error, response)
socket.response(buffer, peer)
Send a response back to a request.
socket.cancel(id)
Cancels a pending request.
socket.on('request', buffer, peer)
Emitted when a new request arrives. Call the above .response
with the same peer object to send a response back to this request.
socket.on('response', buffer, peer)
Emitted when any response arrives.
socket.on('error', err)
Emitted when a critical error happens.
socket.on('warning', err)
Emitted when a non critical error happens (you usually do not need to listen for this).
socket.on('close')
Emitted when the request socket closes (after it is destroyed).
socket.on('listening')
Emitted when the socket is listening.
socket.listen([port|options], [callback])
Listen on a specific port. If port is omitted a random one will be used.
扩展于dgram 的 socket.bind(options, callback)
.options <Object> 必要的。包含以下属性:
port <integer>
address <string>
exclusive <boolean>
callback <Function>
对于 UDP socket,该方法会令dgram.Socket在指定的port和可选的address上监听数据包信息。若port未指定或为 0,操作系统会尝试绑定一个随机的端口。若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.
在配合cluster模块使用dgram.Socket对象时,options对象可能包含一个附加的exclusive属性。当exclusive被设为false(默认值)时,集群工作单元会使用相同的 socket 句柄来共享连接处理作业。当exclusive被设为true时,该句柄将不会被共享,而尝试共享端口则会造成错误。
一个绑定的数据报 socket 会使 Node.js 进程持续运行以接受数据报消息。
如果绑定失败,一个 'error' 事件会产生。在极少数情况下(例如尝试绑定一个已经关闭的 socket), 一个 Error 可能抛出。
一个不共享端口的 socket 的例子如下文所示。
socket.bind({
address: 'localhost',
port: 8000,
exclusive: true
});
socket.destroy()
Completely destroy the request socket (cancels all pending requests).
License
MIT