From 3c0af23842d7c962a0dcbdc62df80d91cc4edc1c Mon Sep 17 00:00:00 2001 From: Steve Oney Date: Mon, 13 Aug 2018 14:00:37 -0400 Subject: [PATCH] rolling websocketjson stream in --- types/sharedb/sharedb-tests.ts | 26 +++++++++++++++++++++++++- types/sharedb/websocketJSONStream.ts | 26 -------------------------- 2 files changed, 25 insertions(+), 27 deletions(-) delete mode 100644 types/sharedb/websocketJSONStream.ts diff --git a/types/sharedb/sharedb-tests.ts b/types/sharedb/sharedb-tests.ts index b156b60965..bec9905050 100644 --- a/types/sharedb/sharedb-tests.ts +++ b/types/sharedb/sharedb-tests.ts @@ -2,7 +2,31 @@ import * as ShareDB from 'sharedb'; import * as http from 'http'; import * as WebSocket from 'ws'; -import { WebSocketJSONStream } from './websocketJSONStream'; +import {Duplex} from 'stream'; + +//Adapted from https://github.com/avital/websocket-json-stream +class WebSocketJSONStream extends Duplex { + constructor(private readonly ws:WebSocket) { + super({objectMode: true}); + this.ws.on('message', (msg: string) => { + this.push(JSON.parse(msg)) + }); + this.ws.on('close', () => { + this.push(null); // end readable stream + this.end(); // end writable stream + + this.emit('close'); + this.emit('end'); + }); + this.on('error', function() { ws.close(); }); + this.on('end', function() { ws.close(); }); + }; + public _read():void {}; + public _write(msg:any, encoding:string, next:Function):void { + this.ws.send(JSON.stringify(msg)); + next(); + }; +}; const backend = new ShareDB(); const connection = backend.connect(); diff --git a/types/sharedb/websocketJSONStream.ts b/types/sharedb/websocketJSONStream.ts deleted file mode 100644 index f21d2fa94e..0000000000 --- a/types/sharedb/websocketJSONStream.ts +++ /dev/null @@ -1,26 +0,0 @@ -import {Duplex} from 'stream'; -import * as WebSocket from 'ws'; - -//Adapted from https://github.com/avital/websocket-json-stream -export class WebSocketJSONStream extends Duplex { - constructor(private readonly ws:WebSocket) { - super({objectMode: true}); - this.ws.on('message', (msg: string) => { - this.push(JSON.parse(msg)) - }); - this.ws.on('close', () => { - this.push(null); // end readable stream - this.end(); // end writable stream - - this.emit('close'); - this.emit('end'); - }); - this.on('error', function() { ws.close(); }); - this.on('end', function() { ws.close(); }); - }; - public _read():void {}; - public _write(msg:any, encoding:string, next:Function):void { - this.ws.send(JSON.stringify(msg)); - next(); - }; -}; \ No newline at end of file