105 lines
1.9 KiB
JavaScript
105 lines
1.9 KiB
JavaScript
const net = require('net');
|
|
const { resolve } = require('path');
|
|
|
|
/**
|
|
|
|
Actions
|
|
|
|
0 = NONE,
|
|
1 = LOAD,
|
|
2 = DISPLAY,
|
|
3 = STOP
|
|
|
|
**/
|
|
|
|
const serverAddress = 'localhost';
|
|
const serverPort = 8081;
|
|
|
|
const client = new net.Socket();
|
|
let sent = 0;
|
|
|
|
console.log(`Connecting to ${serverAddress}:${serverPort}...`);
|
|
|
|
async function delay (ms) {
|
|
return new Promise((resolve, reject) => {
|
|
return setTimeout(resolve, ms);
|
|
});
|
|
}
|
|
|
|
function send (data) {
|
|
const str = JSON.stringify(data);
|
|
sent = +new Date();
|
|
console.log(`[${sent}] SENDING ` + str);
|
|
client.write(str);
|
|
}
|
|
|
|
function actionLoad (img, x, y, w, h) {
|
|
const obj = {
|
|
action : 1, //LOAD
|
|
image : resolve(img),
|
|
position : {
|
|
x,
|
|
y,
|
|
w,
|
|
h
|
|
}
|
|
};
|
|
send(obj);
|
|
}
|
|
|
|
function actionExpose (img, exposure) {
|
|
const obj = {
|
|
action : 2, //DISPLAY
|
|
image : resolve(img),
|
|
exposure : [ exposure ]
|
|
};
|
|
send(obj);
|
|
}
|
|
|
|
function actionStart (img) {
|
|
const obj = {
|
|
action : 2, //DISPLAY
|
|
image : resolve(img)
|
|
};
|
|
send(obj);
|
|
}
|
|
|
|
function actionStop (img) {
|
|
const obj = {
|
|
action : 3, //STOP
|
|
image : resolve(img)
|
|
}
|
|
send(obj);
|
|
}
|
|
|
|
client.connect(serverPort, serverAddress, async () => {
|
|
const img = './img/4kSnake.png';
|
|
actionLoad(img, 0, 0, 320, 320);
|
|
//
|
|
await delay(2000);
|
|
actionExpose(img, 4000);
|
|
//actionStart(img);
|
|
//
|
|
await delay(8000);
|
|
actionLoad(img, 100, 100, 640, 640);
|
|
//
|
|
await delay(1000);
|
|
actionStart(img);
|
|
//
|
|
await delay(2000);
|
|
actionStop(img);
|
|
});
|
|
|
|
client.on('data', (data) => {
|
|
const received = +new Date();
|
|
console.log(`[${received}] RECEIVED ` + data.toString());
|
|
console.log(`DIFF ${received - sent}`);
|
|
});
|
|
|
|
client.on('close', () => {
|
|
console.log('Closing connection');
|
|
});
|
|
|
|
client.on('error', (err) => {
|
|
console.error('Error:', err);
|
|
}); |