forked from neustar/node-coap-dtls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (81 loc) · 2.39 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Copyright (c) 2013-2015 node-coap contributors.
*
* node-coap is licensed under an MIT +no-false-attribs license.
* All rights not explicitly granted in the MIT license are reserved.
* See the included LICENSE file for more details.
*/
const util = require('util');
var optionsConv = require('./lib/option_converter'),
Server = require('./lib/server'),
Agent = require('./lib/agent'),
parameters = require('./lib/parameters'),
net = require('net'),
URL = require('url'),
globalAgent = new Agent({type: 'udp4'}),
globalAgentV6 = new Agent({type: 'udp6'})
/*
* This is the only exposure that the application should have to the
* client-side of the coap implementation. Concentrate parameter-normalization
* code in this call.
*/
module.exports.request = function(url, dtlsOpts, callback) {
var agent, req, ipv6, _dtls
if (typeof url === 'string') {
url = URL.parse(url)
}
//console.log("sending:" + JSON.stringify(url, 4));
ipv6 = net.isIPv6(url.hostname || url.host)
if ((url.protocol === 'coaps:') || (typeof dtlsOpts === 'Object')) {
// DTLS CONFIG
_dtls = {
host: url.hostname,
port: url.port || 5684
};
Object.assign(_dtls, dtlsOpts);
url.agent = new Agent({
type: ipv6 ? 'udp6' : 'udp4',
host: url.hostname,
port: url.port || 5684
},
_dtls,
(ag) => {
var _req = ag.request(url, _dtls);
//console.log(util.inspect(_req));
callback(_req);
//_req.end();
});
// dtls wait
// setTimeout(() => {
// callback(agent.request(url, _dtls))
// }, 10000)
}
else {
// No DTLS. Vanilla datagram.
if (url.agent) {
agent = url.agent
}
else {
agent = ipv6 ? globalAgentV6 : globalAgent
}
if (agent._sock) {
var req = agent.request(url);
req.end();
callback(req);
return req;
}
else {
console.log("Socket is not ready!\n");
}
}
}
module.exports.createServer = Server
module.exports.Agent = Agent
module.exports.globalAgent = globalAgent
module.exports.globalAgentIPv6 = globalAgentV6
module.exports.registerOption = optionsConv.registerOption
module.exports.registerFormat = optionsConv.registerFormat
module.exports.ignoreOption = optionsConv.ignoreOption
module.exports.parameters = parameters
module.exports.updateTiming = parameters.refreshTiming
module.exports.defaultTiming = parameters.defaultTiming