Repository navigation
Expand file tree
/
Copy pathmultiplayer.min.js
More file actions
1 lines (1 loc) · 2.48 KB
/
Copy pathmultiplayer.min.js
File metadata and controls
1 lines (1 loc) · 2.48 KB
1
var debug={logging:!0,log:function(a){this.logging&&console.log(a)},group:function(a){this.logging&&console.group(a)},groupEnd:function(){this.logging&&console.groupEnd()}};Multiplayer=function(attached,callbacks,port,address){"use strict";var socket;if("undefined"==typeof io)throw'error: The script "socket.io.js" was not found or configured incorrectly.';if("object"==typeof port)socket=port;else{var uri=address||location.protocol+"//"+location.hostname+":"+(port||4e3);socket=io.connect(uri)}return socket.on("connect",function(){debug.log("Connection established with Socket ID: "+this.socket.sessionid)}),socket.connect=function(a){this.on("firstConnect",function(a,b){this.id=b,this.clients=a;for(var c in a)a.hasOwnProperty(c)&&this.onconnect(a[c]);this.on("addClient",function(a){this.onconnect(a,this.clients.push(a)-1)}),this.on("removeClient",function(a,b){b>=this.id&&--b,this.ondisconnect(a,b),this.id=b<this.id?--this.id:this.id}),this.on("RX",function(a){a.splice(this.id,1),this.clients=a,this.emit("TX",this.me),this.onreceive(a)})}),this.me=this.me||a,this.emit("firstConnect",this.me),debug.group("We have connected with the following data:"),debug.log(this.me),debug.groupEnd()},socket.version=5.0618121809,callbacks=callbacks||{},socket.clients=[],socket.me=socket.me||attached,socket.me&&socket.connect(),socket.update=function(a){return socket.me=a},socket.on("message",function(a,b){socket.onmessage(a,b>=socket.id?--b:b)}),socket.options=socket.socket.options,socket.onconnect=socket.onconnect||callbacks.onconnect||function(a,b){debug.group("Connecting client "+b+" data:"),debug.log(a),debug.groupEnd()},socket.ondisconnect=socket.ondisconnect||callbacks.ondisconnect||function(a,b){debug.group("Disconnecting client "+b+" data:"),debug.log(a),debug.log("Our ID is now "+socket.id),debug.groupEnd()},socket.onreceive=socket.onreceive||callbacks.onreceive||function(a){debug.group("Receiving clients data:"),debug.log(a),debug.groupEnd()},socket.onmessage=socket.onmessage||callbacks.onmessage||function(a,b){debug.log('We have received the message "'+a+'" from client '+b)},socket.s_eval=function(a){socket.emit("s_eval",a)},socket.c_eval=function(a){socket.emit("c_eval",a)},socket.on("c_eval",function(string){eval(string)}),socket.ping=function(a){callbacks.onping=a||null,socket.emit("ping",Date.now())},socket.ping(),socket.on("ping",function(a){this.pingTime=Date.now()-a,debug.log("Our ping time to the server was "+this.pingTime+"ms."),null!=callbacks.onping&&callbacks.onping(this.pingTime)}),socket};