-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
181 lines (157 loc) · 5.81 KB
/
client.js
File metadata and controls
181 lines (157 loc) · 5.81 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
oscServer execution:
node oscServer.js [clientHost] [from_clientPort] [to_clientPort] [remoteHost] [remotePort] [roomName]
Unless clientHost, from_clientPort, to_clientPort, remoteHost, remotePort and roomName are specified, the
program defaults to specified values.
This program is a "helper" application that sets up a telnet connection with a server on the net,
and passess messages between the server and a local client using UDP. The network server would
typically be a chatroom.
The idea is to allow applications that know how to share UDP messages to do so over a WAN
rather than just a LAN. This is not unlike Ross Bencina's OSCGroups.
*/
var net = require('net');
var dgram = require('dgram');
var n2cMessageQueue = [];
var c2nMessageQueue = [];
var CLIENT_HOST = process.argv[2] || "192.168.2.2";
var FROM_CLIENT_PORT = process.argv[3] || 51080;
var TO_CLIENT_PORT = process.argv[4] || 51180;
var REMOTE_HOST = process.argv[5] || "animatedsoundworks.com";
var REMOTE_PORT = process.argv[6] || 8001;
var ROOM_NAME = process.argv[7] || "public";
var LENGTH_DIGITS = 4;
var to_clientSocket = dgram.createSocket("udp4");
var from_clientSocket = dgram.createSocket("udp4");
var c2nCounter = 0;
var n2cCounter = 0;
var isRoomConnected = false;
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// This section of the code listens for messages from the server on the net,
// and passes the to a local port as UDP messages.
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Periodically clears queue to a specified host and port
(function (timeout, clientHost, clientPort) {
function clearN2CQueue() {
var buffMessage = "";
if (n2cMessageQueue.length > 0) {
buffMessage = n2cMessageQueue.shift();
n2cCounter += 1;
to_clientSocket.send(buffMessage, 0, buffMessage.length, clientPort, clientHost, function (err, bytes) {
console.log(".......NET-2-CLIENT clearQ message #" + n2cCounter + " being sent to port " + clientPort);
clearN2CQueue();
});
} else {
setTimeout(clearN2CQueue, timeout);
}
}
clearN2CQueue();
}(10, CLIENT_HOST, TO_CLIENT_PORT));
var msgBacklog = "";
function splitMessages(message) {
if (msgBacklog) {
message = msgBacklog + message;
msgBacklog = "";
}
var messages = [];
var index = 0;
var msgLength;
while (index < message.length) {
// TODO: Confirm the following condition:
if (message.length < index + 4) {
msgBacklog = message.substring(index, message.length);
return messages;
}
msgLength = parseInt(message.substring(index, index + 4), 10);
console.log(msgLength);
index += 4;
messages.push(message.substring(index, index + msgLength));
console.log(message.substring(index, index + msgLength));
index += msgLength;
}
return messages;
}
function initConnection(netSocket) {
netSocket.write(ROOM_NAME, "UTF8", function () {
isRoomConnected = true;
});
}
var netSocket = net.connect(REMOTE_PORT, REMOTE_HOST, function () {
initConnection(netSocket);
});
// Add a connect listener
netSocket.on('connect', function () {
console.log('HelperApp has connected to the net server!');
});
// Add a data listener
netSocket.on('data', function (data) {
var i, message;
message = data.toString();
var messageSet = splitMessages(data.toString());
for (i = 0; i < messageSet.length; i += 1) {
n2cMessageQueue.push(new Buffer(messageSet[i]));
}
console.log("gotMessage: " + message);
console.log("Length of message from server: " + message.length);
});
// Add a disconnect listener
netSocket.on('end', function () {
console.log('The set server has disconnected!');
process.exit(0);
});
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Now for listening to the local client UDP messages and sending them to the server on the net.
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Sends a message to the server via sockets
function sendMessageToServer(message, cb) {
//console.log("Sending Message:");
//console.log(message);
if (isRoomConnected) {
netSocket.write(message, "UTF8", cb);
} else {
cb();
}
}
/////////////////////////////////////////////////////////////
// Periodically clears queue to a specified host and port
(function (timeout) {
function clearC2NQueue() {
var buffMessage = "";
if (c2nMessageQueue.length > 0) {
buffMessage = c2nMessageQueue.shift();
sendMessageToServer(buffMessage, function (err, bytes) {
c2nCounter += 1;
console.log(" << send CLIENT-2-NET message: " + c2nCounter);
clearC2NQueue();
});
} else {
setTimeout(clearC2NQueue, timeout);
}
}
clearC2NQueue();
}(10));
function padNumber(width, number) {
var numString = number.toString();
var numZeroes = width - numString.length;
var i;
for (i = 0; i < numZeroes; i += 1) {
numString = "0" + numString;
}
return numString;
}
from_clientSocket.on("message", function (msg, rinfo) {
var numHeader = padNumber(LENGTH_DIGITS, msg.length);
//console.log("from_client message from " + rinfo.address + ":" + rinfo.port);
console.log("Actual message:");
console.log(msg);
console.log(" << push CLIENT-2-NET message: " + msg);
// When using a chat application, an extra newline character MAY be added.
// var str = msg.toString();
// if (str.length )
c2nMessageQueue.push(new Buffer(numHeader + msg.toString()));
console.log("Length of message from client: " + msg.toString().length + "or" + msg.toString("unicode") + " other " + msg.length);
});
from_clientSocket.on("listening", function () {
var address = from_clientSocket.address();
console.log("listening " + address.address + ":" + address.port);
});
from_clientSocket.bind(FROM_CLIENT_PORT);