Skip to content
This repository was archived by the owner on Mar 14, 2023. It is now read-only.

Commit a00b922

Browse files
author
clouless
committed
single ws connection per client
1 parent 634c419 commit a00b922

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "kartoffelstampf-server",
33
"description": "stampf it!",
4-
"version": "2.2.0",
4+
"version": "2.3.0",
55
"license": "MIT",
66
"repository": {
77
"type": "git",

src/services/WebSocketCommandService.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import * as WebSocket from 'ws';
1010
import { IServerOptions, Server as WsServer } from 'ws';
1111
import { CommandInstruction } from '../data/CommandInstruction';
1212
import { CompressInstruction } from '../data/CompressInstruction';
13-
import { UploadFileHelper } from './UploadFileHelper';
1413
import { logger } from './LogService';
14+
import { UploadFileHelper } from './UploadFileHelper';
1515

1616
export class WebSocketCommandService {
1717

@@ -52,7 +52,7 @@ export class WebSocketCommandService {
5252
private init() {
5353
const self = this;
5454
self.wss.on('connection', (ws: WebSocket) => {
55-
ws.on('message', (message) => {
55+
ws.on('message', (message: any) => {
5656
logger.log('debug', 'received command instruction', message);
5757
// Interpret which command to execute => Prevent XSS Injects!
5858
const compressInstruction = JSON.parse(message) as CompressInstruction;
@@ -71,6 +71,7 @@ export class WebSocketCommandService {
7171
payload: {
7272
text: commandInstruction.command + ' ' + commandInstruction.commandArguments.join(' '),
7373
},
74+
compressInstruction: compressInstruction,
7475
type: 'cmd',
7576
}));
7677
cmd.on('error', (error: any) => {
@@ -79,6 +80,7 @@ export class WebSocketCommandService {
7980
payload: {
8081
text: error.toString(),
8182
},
83+
compressInstruction: compressInstruction,
8284
type: 'stderr',
8385
}));
8486
} catch (error) {
@@ -91,6 +93,7 @@ export class WebSocketCommandService {
9193
payload: {
9294
text: data.toString(),
9395
},
96+
compressInstruction: compressInstruction,
9497
type: 'stdout',
9598
}));
9699
} catch (error) {
@@ -103,6 +106,7 @@ export class WebSocketCommandService {
103106
payload: {
104107
text: data.toString(),
105108
},
109+
compressInstruction: compressInstruction,
106110
type: 'stderr',
107111
}));
108112
} catch (error) {
@@ -116,11 +120,15 @@ export class WebSocketCommandService {
116120
payload: {
117121
compressedSize: sizeInBytes,
118122
},
123+
compressInstruction: compressInstruction,
119124
type: 'compressResult',
120125
}));
121-
// Cleanup is done in UploadFileHelper => one hour after upload file is deleted
122-
// close after command is done
123-
ws.close();
126+
// We unsubscribe on DONE in Frontend
127+
ws.send(JSON.stringify({
128+
payload: { },
129+
compressInstruction: compressInstruction,
130+
type: 'DONE',
131+
}));
124132
} catch (error) {
125133
logger.log('error', 'failed to send ws', error);
126134
}

0 commit comments

Comments
 (0)