-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
60 lines (47 loc) · 1.31 KB
/
client.js
File metadata and controls
60 lines (47 loc) · 1.31 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
var Server = require('./server.js');
var dgram = require('dgram');
/* client.js */
var Client = function () {
// create server
// server creates its own socket
this.server = new Server();
// config
this.port = 3000;
this.host = '127.0.0.1';
// create socket
this.socket = dgram.createSocket('udp4');
};
Client.prototype.send = function(operator,operand1,operand2) {
// convert operator to byte
switch(operator) {
case '+':
operator_byte = 1;
break;
case '-':
operator_byte = 2;
break;
case '/':
operator_byte = 3;
break;
case 'x':
operator_byte = 4;
break;
default:
operator_byte = 0;
}
// put operator & operands into buffer
var msg = new Buffer([operator_byte,operand1,operand2]);
var that = this;
// send buffer to server
this.socket.send(msg, 0, msg.length, this.port, this.host, function(error) {
if(error) console.log(error);
else {
console.log("Send message to " + that.host + ":" + that.port);
console.log(msg);
}
// close socket after message is sent
that.socket.close();
});
}
// export client as module
module.exports = Client;