-
Notifications
You must be signed in to change notification settings - Fork 0
/
sb-1.4.1.min.js
1 lines (1 loc) · 8.07 KB
/
sb-1.4.1.min.js
1
if(!Function.prototype.bind){Function.prototype.bind=function(e){if(typeof this!=="function"){throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable")}var t=Array.prototype.slice.call(arguments,1),n=this,r=function(){},i=function(){return n.apply(this instanceof r?this:e||window,t.concat(Array.prototype.slice.call(arguments)))};r.prototype=this.prototype;i.prototype=new r;return i}}var Spacebrew=Spacebrew||{};var WebSocket=WebSocket||{};var window=window||undefined;var module=module||undefined;if(window){if(!window["getQueryString"]){window.getQueryString=function(e){if(!window.location){return}e=e.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");var t="[\\?&]"+e+"=([^&#]*)";var n=new RegExp(t);var r=n.exec(window.location.href);if(r==null){return""}else{return r[1]}}}}if(!window&&module){WebSocket=require("ws");module.exports={Spacebrew:Spacebrew}}Spacebrew.Client=function(e,t,n,r){r=r||{};if(e!==undefined){if(toString.call(e)!=="[object String]"){r.port=e.port||undefined;r.debug=e.debug||false;r.reconnect=e.reconnect||false;n=e.description||undefined;t=e.name||undefined;e=e.server||undefined}}this.debug=false;if(window){this.debug=window.getQueryString("debug")==="true"?true:r.debug||false}this.reconnect=r.reconnect||true;this.reconnect_timer=undefined;this.sendRateCapped=r.capSendRate===undefined?false:r.capSendRate;this.send_interval=r.sendRate||16;this.send_blocked=false;this.msg={};this._name=t||"javascript client #";if(window){this._name=window.getQueryString("name")!==""?unescape(window.getQueryString("name")):this._name}this._description=n||"spacebrew javascript client";if(window){this._description=window.getQueryString("description")!==""?unescape(window.getQueryString("description")):this._description}this.server=e||"sandbox.spacebrew.cc";if(window){this.server=window.getQueryString("server")!==""?unescape(window.getQueryString("server")):this.server}this.port=r.port||9e3;if(window){var i=window.getQueryString("port");if(i!==""&&!isNaN(i)){this.port=i}}this.socket=null;this.client_config={name:this._name,description:this._description,publish:{messages:[]},subscribe:{messages:[]},options:{}};this.admin={};this._isConnected=false};Spacebrew.Client.prototype.connect=function(){try{this.socket=new WebSocket("ws://"+this.server+":"+this.port);this.socket.binaryType="arraybuffer";this.socket.onopen=this._onOpen.bind(this);this.socket.onmessage=this._onMessage.bind(this);this.socket.onclose=this._onClose.bind(this)}catch(e){this._isConnected=false;console.log("[connect:Spacebrew] connection attempt failed")}};Spacebrew.Client.prototype.close=function(){try{if(this._isConnected){this.socket.close();this._isConnected=false;console.log("[close:Spacebrew] closing websocket connection")}}catch(e){this._isConnected=false}};Spacebrew.Client.prototype.onOpen=function(e,t){};Spacebrew.Client.prototype.onClose=function(e,t){};Spacebrew.Client.prototype.onRangeMessage=function(e,t){};Spacebrew.Client.prototype.onBooleanMessage=function(e,t){};Spacebrew.Client.prototype.onStringMessage=function(e,t){};Spacebrew.Client.prototype.onCustomMessage=function(e,t,n){};Spacebrew.Client.prototype.onBinaryMessage=function(e,t,n){};Spacebrew.Client.prototype.addPublish=function(e,t,n){this.client_config.publish.messages.push({name:e,type:t,"default":n});this.updatePubSub()};Spacebrew.Client.prototype.addSubscribe=function(e,t){this.client_config.subscribe.messages.push({name:e,type:t});this.updatePubSub()};Spacebrew.Client.prototype.updatePubSub=function(){if(this._isConnected){this.socket.send(JSON.stringify({config:this.client_config}))}};Spacebrew.Client.prototype.send=function(e,t,n){var r=this;this.msg={message:{clientName:this._name,name:e,type:t}};if(typeof n=="string"){this.msg.message.value=n}else{if("buffer"in n&&n.buffer instanceof ArrayBuffer){n=n.buffer}if(n instanceof ArrayBuffer){this.msg.message.value=n.byteLength}else{return}}if(this.sendRateCapped){if(!this.send_blocked){this.socket.send(JSON.stringify(this.msg));this.send_blocked=true;this.msg=undefined;setTimeout(function(){r.send_blocked=false;if(r.msg!==undefined){r.send(r.msg.message.name,r.msg.message.type,r.msg.message.value)}},r.send_interval)}}else{if(n instanceof ArrayBuffer){var i=JSON.stringify(this.msg);var s=Spacebrew.numBytesToEncodeString(i);var o=s<254?1:s<=65535?3:5;var u=n.byteLength+s+o;var a=new ArrayBuffer(u);var f=new Uint8Array(a,0,o);if(o==5){f[0]=255;f[1]=s>>>24&255;f[2]=s>>>16&255;f[3]=s>>>8&255;f[4]=s&255}else if(o==3){f[0]=254;f[1]=s>>>8&255;f[2]=s&255}else{f[0]=s}Spacebrew.encodeToBuffer(i,new Uint8Array(a,o,s));var l=new Uint8Array(a,o+s);l.set(new Uint8Array(n));this.socket.send(a)}else{this.socket.send(JSON.stringify(this.msg))}}};Spacebrew.Client.prototype._onOpen=function(){console.log("[_onOpen:Spacebrew] Spacebrew connection opened, client name is: "+this._name);this._isConnected=true;if(this.admin.active){this.connectAdmin()}if(this.reconnect_timer){console.log("[_onOpen:Spacebrew] tearing down reconnect timer");this.reconnect_timer=clearInterval(this.reconnect_timer);this.reconnect_timer=undefined}this.updatePubSub();this.onOpen()};Spacebrew.Client.prototype._onMessage=function(e){var t,n;if(e.data instanceof ArrayBuffer){var r=new Uint8Array(e.data);var i=r[0];var s=1;if(i==254){i=(r[1]<<8)+r[2];s=3}else if(i==255){i=(r[1]<<24)+(r[2]<<16)+(r[3]<<8)+r[4];s=5}if(i>0){try{var o=Spacebrew.decodeFromBuffer(new Uint8Array(e.data,s,i));t=JSON.parse(o);n={buffer:e.data,startIndex:s+i}}catch(u){console.error(u);return}}else{return}}else{t=JSON.parse(e.data)}var a,f,l,c;if(!("targetType"in t)&&!(t instanceof Array)||t["targetType"]=="client"){if("message"in t){a=t.message.name;f=t.message.type;l=t.message.value;if(t.message.clientName){c=t.message.clientName}if(n!==undefined){this.onBinaryMessage(a,n,f)}else{switch(f){case"boolean":this.onBooleanMessage(a,l=="true");break;case"string":this.onStringMessage(a,l);break;case"range":this.onRangeMessage(a,Number(l));break;default:this.onCustomMessage(a,l,f)}}}else{return}}else{if(this.admin){this._handleAdminMessages(t)}}};Spacebrew.Client.prototype._onClose=function(){var e=this;console.log("[_onClose:Spacebrew] Spacebrew connection closed");this._isConnected=false;if(this.admin.active){this.admin.remoteAddress=undefined}if(this.reconnect&&!this.reconnect_timer){console.log("[_onClose:Spacebrew] setting up reconnect timer");this.reconnect_timer=setInterval(function(){if(e.isConnected!==false){e.connect();console.log("[reconnect:Spacebrew] attempting to reconnect to spacebrew")}},5e3)}this.onClose()};Spacebrew.Client.prototype.name=function(e){if(e){if(this._isConnected){return false}this._name=e;if(window){this._name=window.getQueryString("name")!==""?unescape(window.getQueryString("name")):this._name}this.client_config.name=this._name}return this._name};Spacebrew.Client.prototype.description=function(e){if(e){if(this._isConnected){return false}this._description=e||"spacebrew javascript client";if(window){this._description=window.getQueryString("description")!==""?unescape(window.getQueryString("description")):this._description}this.client_config.description=this._description}return this._description};Spacebrew.Client.prototype.isConnected=function(){return this._isConnected};Spacebrew.Client.prototype.extend=function(e){for(var t in e){if(e.hasOwnProperty(t)){this[t]=e[t]}}};Spacebrew.numBytesToEncodeString=function(e){var t=0;for(var n=e.length-1;n>=0;n--){t+=Spacebrew.numBytesToEncodeCode(e.charCodeAt(n))}return t};Spacebrew.numBytesToEncodeCode=function(e){if(e>>16>0){return 4}if(e>>11>0){return 3}if(e>>7>0){return 2}return 1};Spacebrew.encodeToBuffer=function(e,t){if(!(t instanceof Uint8Array)){var n=new ArrayBuffer(Spacebrew.numBytesToEncodeString(e));t=new Uint8Array(n)}var r,i,s=0;for(var o=0;o<e.length;o++){i=e.charCodeAt(o);r=Spacebrew.numBytesToEncodeCode(i);if(r==1){t[s]=i;s++}else{var u=128;for(var a=1;a<r;a++){u=u>>1|128;t[s+r-a]=128|i&63;i>>>=6}t[s]=u|i;s+=r}}return t};Spacebrew.decodeFromBuffer=function(e){if(!(e instanceof Uint8Array)){return undefined}var t,n,r,i="";for(var s=0;s<e.length;){r=e[s];s++;n=0;while((r&128)>0){n++;r=r<<1&255}t=r>>>n;if(n===0){n=1}for(var o=1;o<n;o++){t=t<<6|e[s]&63;s++}i+=String.fromCharCode(t)}return i}