//the main javascript file

//var conn = new WebSocket('ws://127.0.0.1:8890');
//var x;
var buffer = {
    data: [],
    finished: false,
    push: function(x) {
        if(this.finished) {
            this.finished = false;
            this.data = [];
        }
        this.data.push(x);
    }
}
var conn = {
    ws: new WebSocket('ws://127.0.0.1:8890'),
    ret: '' }

var Msg = { //creating the JSON
    create: function(type, msg) {
        if(type != 'connection' &&
            type != 'message' &&
            type != 'dirlist') {
            throw 'invalid message';
        }
        var ret = {
            type: type,
            message: msg || ''};
        return JSON.stringify(ret);
    },
    parse: function(msg) {
        try {
            var parsed = JSON.parse(msg);
            return parsed;
        } catch (e) {
            throw e;
        }
    }
}

conn.ws.onopen = function() { console.log("connection opened");};
conn.ws.onerror = function() { console.log("error");};
conn.ws.onmessage = function(msg) {
    if(msg.data instanceof Blob) {
        buffer.push(msg.data);
        return 0;
    }
    conn.ret = handle(Msg.parse(msg.data));
};
function handle(options) {
    switch(options.type) { 
        case 'buffer_reset':
            buffer.finished = true;
            break;
        default:
            return options;
    }
}

@@ -1,92 +1,4 @@
-//the main javascript file%0A%0A//var conn = new WebSocket('ws://127.0.0.1:8890');%0A//var x;%0A
 var 
@@ -273,17 +273,17 @@
  ret: ''
- 
+%0A
 %7D%0A%0Avar M
@@ -407,24 +407,62 @@
 message' &&%0A
+            type != 'start_buffer' &&%0A
             
@@ -603,16 +603,25 @@
 sg %7C%7C ''
+%0A        
 %7D;%0A     
@@ -681,24 +681,92 @@
 tion(msg) %7B%0A
+        if(msg === '') return ''; //empty string was throwing error%0A
         try 
@@ -935,38 +935,50 @@
  con
-sole.log(%22connection opened%22
+n.ws.send(Msg.create('start_buffer','')
 );
+ 
 %7D;%0Ac
@@ -1227,24 +1227,24 @@
 (options) %7B%0A
+
     switch(o
@@ -1257,17 +1257,16 @@
 .type) %7B
- 
 %0A       

fix error where JSON.parse was trying to parse empty string