java - How do I test my WebSocket which is developed in JavaScript -


i have created websocket.js unfortunately unable test due unavailability of data. requirement data comes various sources have multiple sockets that. note: want particularly test socket.onmessage behaves different sockets. please find code snippet below:

var websocket; var txqueue = []; var defaultreconnecttimeout = 1000; //will multiplied 2 , saved reconnecttimeout on each try var reconnecttimeout = defaultreconnecttimeout;  var registerwebsockethandlers = function(websocket) {     websocket.onclose = function(){         settimeout(service.reopen, reconnecttimeout *= 2);     };      websocket.onopen = function(e) {         reconnecttimeout = defaultreconnecttimeout; //reset         deferredsend();     };      websocket.onerror = function(e) {         throw new error("[websocket] error occured " + e);     }; }  var uniqid = function() {     return (new date().gettime()).tostring(16); }   var deferredsend = function() {     if(!service.isopen()) {         $timeout(deferredsend, 100);         return;     }     while(txqueue.length && service.isopen()) {         var payload = txqueue.shift();         websocket.send(typeof payload === 'string' ? payload : json.stringify(payload));     } };  var createnewwebsocketinstance = function(apiurl){     var websocket = new $window.websocket(apiurl);     websocket.id = uniqid();     return websocket; }  // todo: bit hacky since directly bind raw window event $window.onbeforeunload = function(e) {     service.close(); };  var service = {};  service.setmessageeventhandler = function(name,cb) {     instances[name].onmessage = function(msg) {         if(msg.data.indexof('status: connected') === 0)         {             return;         }          var jsonobj = json.parse(msg.data);         cb(jsonobj);     }; };  service.isopen = function() {     return websocket.readystate === 1; };  service.send = function(msg) {     txqueue.push(msg);     deferredsend(); };  service.close = function() {     return websocket.close(); };  service.reopen = function() {     // old message handler     var msghandler = websocket.onmessage;      // try closing previous websocket     service.close();      // open new websocket     openconnection();      // re-attach old handler new websocket     websocket.onmessage = msghandler; };  service.getid = function() {     return websocket.id; }  // returns existing instance of socket, if unavailable creates new one.       service.getinstance = function(name, config) {     if(!(name in instances)) {         instances[name] = createnewwebsocketinstance(config);     }      registerwebsockethandlers(instances[name]);     return instances[name]; };  return service; 

you can test websocket using websocket.html @ websocket.org echo test creating own test

using text editor, copy following code , save websocket.html somewhere on hard drive. open in browser. page automatically connect, send message, display response, , close connection.

see linux - websocket test error.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -