-
Notifications
You must be signed in to change notification settings - Fork 0
/
udpclient.js
121 lines (99 loc) · 2.73 KB
/
udpclient.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"use strict";
var dgram = require('dgram');
var HOST = '0.0.0.0';
var BroadCastTo = '0.0.0.0';
var server = undefined;
var BroadcastPort;
var remoteSocket = null;
var Me = this;
exports.startServer = function (host, port)
{
BroadcastPort = port;
_udp_server (host, port);
}
exports.onMessageReceived = undefined;
exports.sendMessage = function (message)
{
_udp_client (message);
}
var _udp_server = function (host,port)
{
server = dgram.createSocket('udp4');
server.on('listening', function () {
var address = host;
console.log('UDP Listener Active');
});
server.on('message', function (message, remote) {
/*try
{
var mavmsg = MAV.decode(message);
if (mavmsg.name == 'HEARTBEAT')
{
return
}
}
catch (ex)
{
console.log ("error: %s",ex.message);
}*/
remoteSocket = remote;
if (Me.onMessageReceived!= undefined)
{
Me.onMessageReceived (message);
}
});
server.bind(port, host);
// console.log ("UDP Listener at " + host + ":" + port);
}
var _udp_client = function (msg)
{
//if (remoteSocket == null) return;
//server.send(message, 0, message.length, remoteSocket.port, remoteSocket.address, function(err, bytes) {
//console.log(MAV.decode(message));
/* MAV.on('message', function (e)
{
e.pack(MAV);
console.log (e);
var mm = e.msgbuf;
}); */
/*var beacon = new mavlink.messages.beacon_pos(1000,1000,200,10);
var b = beacon.pack(MAV);
b[0] = 254;
Me.onMessageReceived (b);
*/
/*
var mavmsg = null
try {
mavmsg = MAV.decode(message);
}
catch (e)
{
}
if (mavmsg == null) return ;
//var enc = mavmsg.pack(MAV);
var enc = mavmsg.pack(MAV);//,mavmsg.crc_extra,mavmsg.payload);
///console.log(message);
///console.log(mavmsg);
enc[0] = 254;
var msg = new Buffer(enc);; //enc.join("");
console.log("MSG IS" + msg);
*/
if (Listener === True)
{
if (remoteSocket != null)
{
server.send(msg, 0, msg.length, remoteSocket.port, remoteSocket.address, function(err, bytes) {
//if (err) throw err;
//console.log('UDP message sent to ' + '0.0.0.0' +':'+ BroadcastPort);
});
}
}
else
{
server.setBroadcast(true);
server.send(msg, 0, msg.length, BroadcastPort, Target_IP, function(err, bytes) {
//if (err) throw err;
//console.log('UDP message sent to ' + '0.0.0.0' +':'+ BroadcastPort);
});
}
}