mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2024-12-29 08:33:20 -05:00
1 line
1.5 KiB
JavaScript
1 line
1.5 KiB
JavaScript
var MeshServerCreateControl=function(t,e){var o={State:0,connectstate:0,pingTimer:null};return o.authCookie=e,o.xxStateChange=function(e,t){var n;o.State!=e&&(n=o.State,o.State=e,o.onStateChanged)&&o.onStateChanged(o,o.State,n,t)},o.Start=function(){var e;0==o.connectstate&&(o.connectstate=0,e=window.location.protocol.replace("http","ws")+"//"+window.location.host+t+"control.ashx"+(urlargs.key?"?key="+urlargs.key:""),o.authCookie&&""!=o.authCookie&&(e+="?moreargs=1"),o.socket=new WebSocket(e),o.socket.onopen=function(e){o.connectstate=1,o.authCookie&&""!=o.authCookie&&o.send({action:"urlargs",args:{auth:o.authCookie}})},o.socket.onmessage=o.xxOnMessage,o.socket.onclose=function(e){o.Stop(e.code)},o.xxStateChange(1,0),null!=o.pingTimer&&clearInterval(o.pingTimer),o.pingTimer=setInterval(function(){o.send({action:"ping"})},29e3))},o.Stop=function(e){o.connectstate=0,o.socket&&(o.socket.close(),delete o.socket),null!=o.pingTimer&&(clearInterval(o.pingTimer),o.pingTimer=null),o.xxStateChange(0,e)},o.xxOnMessage=function(e){var t;1==o.State&&o.xxStateChange(2);try{t=JSON.parse(e.data)}catch(e){return}"object"==typeof t&&"pong"!=t.action&&("ping"==t.action&&o.send({action:"pong"}),"close"==t.action?(t.msg&&console.log(t.msg),o.Stop(t.cause)):(1==o.trace?console.log("RECV",t):2==o.trace&&console.log("RECV",JSON.stringify(t)),o.onMessage&&o.onMessage(o,t)))},o.send=function(e){null!=o.socket&&1==o.connectstate&&("ping"!=e.action&&(1==o.trace?console.log("SEND",e):2==o.trace&&console.log("SEND",JSON.stringify(e))),o.socket.send(JSON.stringify(e)))},o} |