forked from cScarlson/sawkit-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path$ws-min.js
1 lines (1 loc) · 2.43 KB
/
$ws-min.js
1
Function.prototype.method=function(e,t){this.prototype[e]=t;return this};String.prototype.forEach=Array.prototype.forEach;var $ws=window.$ws=window.$ws||function(){function e(){var e=this;return function(n){e.ws=new WebSocket(n||"localhost:9696"||"ws://e.ws.ndn.ucla.edu:9696");return{ws:e.ws,ready:e.connected,send:e.send,emit:e.emit,sendBinary:e.sendBinary,on:e.on,settings:e.settings}}}e.prototype=function(){function i(e){t=this;this.ws.onopen=function(n){console.log("WebSocket Connection Made. Event:",n);e(t,t.ws)};this.ws.onmessage=function(e){t.on({data:e})};return this}function s(e,t){this.settings({binaryType:"arraybuffer"});this.ws.send(e)}function o(e,t){if(!e)throw new Error("Error: no data exists to send.");e&&t&&this.emit("message",e,t);!t&&this.emit("message",e);return this}function u(e,t,n){if(!t||t.constructor===Function)throw new Error("Error: data-parameter is absent or is a function.");var i=t&&r.msgBridge({action:"send",data:t,event:e}).data;this.ws.send(i);console.log("message sent...");n&&n(true);return this}function a(e,t){var n,i,s=e.constructor;s===String&&t&&function(){i=r.pro.mise({event:e,callback:t})}()||s===Object&&!t&&function(){n=r.msgBridge({action:"receive",data:e.data}).promise;n.callback&&n.callback(n.data,n.raw,n)}()||(s===ArrayBuffer||s===Uint8Array)&&!t&&function(){n=r.msgBridge({action:"receive",data:e.data}).promise;n.callback&&n.callback(n.data,n.raw,n)}();return this}function f(e){var t=[];for(opt in e){this.ws[opt]=e[opt];t.push(opt)}console.log("settings changed for each of these:",t,this.ws);return this}function l(){}function c(){console.log("close:",e);return this}var t,n={};var r=function(){function n(e){var t={},e=e,n=e.data,i=e.event;e.action==="send"&&function(){i==="message"&&function(){n=JSON.stringify(n)}()||i!=="message"&&function(){n=JSON.stringify({_data:n,_event:i})}()}()||e.action==="receive"&&function(){var e=n,i="message",s=n.data.constructor,o=s!==ArrayBuffer&&s!==Uint8Array&&s!==Blob&&JSON.parse(e.data)||n.data;o.constructor===Object&&function(){i=o._event&&o._event||i;o=o._data&&o._data||o}();r.pro.gress(i,function(n){n.data=o,n.raw=e,n.event=i;t=n})}();return{data:n,promise:t}}var e=(new Date).getTime();var t=function(){function t(t){e.push(t);return e}function n(t,n){e.forEach(function(e,r,i){e.event===t&&n(e)})}var e=e||[];return{promises:e,mise:t,gress:n}}();return{ts:e,pro:t,msgBridge:n}}();return{connected:i,send:o,emit:u,sendBinary:s,on:a,settings:f,get:l}}();return new e}()