-
Notifications
You must be signed in to change notification settings - Fork 0
/
conductor.min.js
4 lines (3 loc) · 2.81 KB
/
conductor.min.js
1
2
3
4
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
"use strict";function _classCallCheck(e,n){if(!(e instanceof n))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(exports,"__esModule",{value:!0});var _createClass=function(){function e(e,n){for(var s=0;s<n.length;s++){var t=n[s];t.enumerable=t.enumerable||!1,t.configurable=!0,"value"in t&&(t.writable=!0),Object.defineProperty(e,t.key,t)}}return function(n,s,t){return s&&e(n.prototype,s),t&&e(n,t),n}}(),Conductor=function(){function e(n,s,t){_classCallCheck(this,e),this.channels={},this.isConnected=!1,this.kAllMessages="*",this.ConOpCode=Object.freeze({Bind:1,Unbind:2,Write:3,Info:4,Server:7,Invite:8});var i="?Token";n.indexOf(i)>-1&&(i="&");var o=this;this.socket=new WebSocket(n+i+"="+s),this.socket.onopen=function(e){o.isConnected=!0,null!=t&&t(o.isConnected)},this.socket.onclose=function(e){o.isConnected=!1,null!=t&&t(o.isConnected)},this.socket.onerror=function(e){console.log("Error detected: "+e)},this.socket.onmessage=function(e){o.processMessage(e.data)}}return _createClass(e,[{key:"bind",value:function(e,n){this.channels[e]=n,e!=this.kAllMessages&&this.writeMessage("",e,this.ConOpCode.Bind,null)}},{key:"unbind",value:function(e){delete this.channels[e],e!=this.kAllMessages&&this.writeMessage("",e,this.ConOpCode.Unbind,null)}},{key:"serverBind",value:function(e){this.serverChannel=e}},{key:"serverUnBind",value:function(e){this.serverChannel=null}},{key:"sendMessage",value:function(e,n,s){this.writeMessage(e,n,this.ConOpCode.Write,s)}},{key:"sendInfo",value:function(e,n,s){this.writeMessage(e,n,this.ConOpCode.Info,s)}},{key:"sendInvite",value:function(e,n,s){this.writeMessage(e,n,this.ConOpCode.Invite,s)}},{key:"sendServerMessage",value:function(e,n,s){this.writeMessage(e,n,this.ConOpCode.Server,s)}},{key:"disconnect",value:function(){this.isConnected&&(this.channels={},this.socket.close())}},{key:"writeMessage",value:function(e,n,s,t){var i={body:e,channel_name:n,opcode:s,additional:t};this.socket.send(JSON.stringify(i))}},{key:"processMessage",value:function(e){var n=JSON.parse(e);n.opcode==this.ConOpCode.Server||n.opcode==this.ConOpCode.Invite?null!=this.serverChannel&&this.serverChannel(n):(null!=this.channels[n.channel_name]&&this.channels[n.channel_name](n),null!=this.channels[this.kAllMessages]&&this.channels[this.kAllMessages](n))}}]),e}();exports.Conductor=Conductor,window.Conductor=Conductor;
},{}]},{},[1]);