feat: hex or utf8 automatic conversion for server traces

This commit is contained in:
Fabio Di Stasio 2023-04-16 10:47:09 +02:00
parent 45c9537cc9
commit 46e9d157d6
6 changed files with 18 additions and 9 deletions

View File

@ -9,7 +9,7 @@ export interface ClientHost {
export interface ClientMessage { export interface ClientMessage {
enabled: boolean; enabled: boolean;
format: 'hex' | 'ascii' | 'binary'; format: 'hex' | 'utf-8' | 'binary';
message: string; message: string;
name: string; name: string;
} }

View File

@ -142,8 +142,8 @@ class Sender {
let msg; let msg;
switch (this.messages[index].format) { switch (this.messages[index].format) {
case 'ascii': case 'utf-8':
msg = Buffer.from(this.messages[index].message, 'ascii'); msg = Buffer.from(this.messages[index].message, 'utf-8');
break; break;
case 'hex': case 'hex':
msg = Buffer.from(this.messages[index].message.replace(/\s|0x/g, ''), 'hex'); msg = Buffer.from(this.messages[index].message.replace(/\s|0x/g, ''), 'hex');

View File

@ -75,7 +75,16 @@ class Server {
this.sendLog(null, '', 'clientConnectedOnPort', { port }); this.sendLog(null, '', 'clientConnectedOnPort', { port });
socket.on('data', (msg: Buffer) => { socket.on('data', (msg: Buffer) => {
const msgString = msg.toString(); let msgString: string;
try {
new TextDecoder('utf8', { fatal: true }).decode(msg);
msgString = msg.toString('utf-8');
}
catch (err) {
msgString = msg.toString('hex');
}
if (this.echo) socket.write(msg); if (this.echo) socket.write(msg);
this.nBytes[i] += msg.length; this.nBytes[i] += msg.length;
this.nMsgs[i]++; this.nMsgs[i]++;

View File

@ -27,8 +27,8 @@
<option value="" disabled> <option value="" disabled>
{{ t('word.select') }} {{ t('word.select') }}
</option> </option>
<option value="ascii"> <option value="utf-8">
ASCII UTF-8
</option> </option>
<option value="hex"> <option value="hex">
HEX HEX

View File

@ -27,8 +27,8 @@
<option value="" disabled> <option value="" disabled>
{{ t('word.select') }} {{ t('word.select') }}
</option> </option>
<option value="ascii"> <option value="utf-8">
ASCII UTF-8
</option> </option>
<option value="hex"> <option value="hex">
HEX HEX

View File

@ -13,7 +13,7 @@ export const useClientStore = defineStore('client', {
}]) as ClientHost[], }]) as ClientHost[],
messages: persistentStore.get('messages', [{ messages: persistentStore.get('messages', [{
enabled: true, enabled: true,
format: 'ascii', format: 'utf-8',
message: 'Hello, World!', message: 'Hello, World!',
name: 'Hello, World!' name: 'Hello, World!'
}]) as ClientMessage[] }]) as ClientMessage[]