Skip to content

Commit

Permalink
Use newline separator and handle large data
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyash-b committed Jun 5, 2022
1 parent 2563ad6 commit 519b617
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@ wss.on('connection', (ws: WebSocket) => {

upstream.on('data', function (data) {
console.log('data from upstream', data);
const msg = JSON.parse(data.toString());
console.log('data from upstream - decoded', msg);
try {
const msg = JSON.parse(data.toString());
console.log('data from upstream - decoded', msg);
} catch(e) {
console.log('data from upstream - could not decode');
}
ws.send(data.toString());
});

}

debug('connection');
ws.send(JSON.stringify({'type': 'please_auth'}));
ws.send(JSON.stringify({'type': 'please_auth'}) + "\n");
ws.onmessage = function (event) {
if (typeof event.data !== "string") {
console.log('event.data is not a string')
Expand All @@ -51,16 +55,16 @@ wss.on('connection', (ws: WebSocket) => {
// maybe implement as JSON RPC too?
if (msg.type === 'auth') {
if (msg.code === code) {
ws.send(JSON.stringify({'type': 'auth_ok'}));
ws.send(JSON.stringify({'type': 'auth_ok'}) + "\n");
authenticated = true;
connect_to_upstream();
} else {
ws.send(JSON.stringify({'type': 'auth_fail'}));
ws.send(JSON.stringify({'type': 'auth_fail'}) + "\n");
}
return;
}
if (!authenticated) {
ws.send(JSON.stringify({'type': 'please_auth', 'hint': 'forbidden'}));
ws.send(JSON.stringify({'type': 'please_auth', 'hint': 'forbidden'}) + "\n");
return;
}
if (msg.jsonrpc === '2.0') {
Expand Down

0 comments on commit 519b617

Please sign in to comment.