-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
46 lines (35 loc) · 893 Bytes
/
client.js
File metadata and controls
46 lines (35 loc) · 893 Bytes
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
// Little test script
const dgram = require( 'dgram' );
var port = '10101';
var host = '127.0.0.1';
var type = 'udp4';
var client = dgram.createSocket( type );
client.on( 'message', onServerMessage );
var commandList = [
"REGISTER|7001;zank",
"SETNAME|My Zank game",
"GAMES|list;zank",
"UNREGISTER|7001"
];
send( commandList[ 0 ] );
function onServerMessage( msg, info ) {
console.log( msg.toString() + '\n' );
}
function send( message, cb ) {
var cbFunc = defaultCallback;
if ( cb ) cbFunc = cb;
var buf = new Buffer( message );
client.send( buf, 0, buf.length, port, host, cbFunc );
}
function defaultCallback( err, bytes ) {
if ( err ) throw err;
console.log( 'OUT: ' + commandList[ 0 ] );
setTimeout( function() {
if ( commandList.length > 1 ) {
commandList = commandList.splice( 1 );
send( commandList[ 0 ] );
} else {
client.close();
}
}, 2000 );
}