socket.io && node.js

by sirfizx

HTML

<script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>

JavaScript

// initialize socket
var socket = io.connect('http://3dp-elhs.rhcloud.com:8000');

var Player = function(playerData){
	this.playerData = playerData;
	var div = document.createElement("DIV");
	div.style.position = 'absolute';
	div.style.width = '128px';
	div.style.height = '128px';
	div.style.backgroundColor = 'pink';
	var h3 = document.createElement("H3");
	this.name = playerData.name;
	h3.innerHTML = this.name;
	h3.style.margin = '20px';
	div.appendChild(h3);
    this.country_div = document.createElement("DIV");
    this.ip_div = document.createElement("DIV");
    this.img_elem = document.createElement("IMG");
    div.appendChild(this.country_div);
    div.appendChild(this.ip_div);
    div.appendChild(this.img_elem);
	this.e = div;
	document.body.appendChild(this.e);
	this.playerData.x = 0;
	this.playerData.y = 0;
	this.playerData.speed = 6;
	this.playerData.staleCount = 0;
    this.playerData.imgURL = '';
    this.playerData.ip = '';
    this.playerData.country = '';
    
	
}

Player.prototype ={
	update: function(playerData,otherPlayers){
     // not implemented
		
	},
	move: function(dx,dy){
		this.playerData.x += dx;
		this.playerData.y += dy;
		this.e.style.top = this.playerData.y + "px";
		this.e.style.left = this.playerData.x + "px";
		//console.log('moving player named ' + this.name);
	},
	destroy: function(){
		if(isNode(this.e)){
			document.body.removeChild(this.e);
			this.e = 0;
		}
	}
}

//HELPERS
//Returns true if it is a DOM node
function isNode(o){
  return (
    typeof Node === "object" ? o instanceof Node : 
    o && typeof o === "object" && typeof o.nodeType === "number" && typeof o.nodeName==="string"
  );
}


var otherPlayers = [];

	var myName = '';
	var myPlayer = '';

socket.on('uuid', function(uuid){
		if(myName == ''){
			myName = prompt("Enter a name for your player.");

			myPlayer = new Player({uuid: uuid, name:myName});

			socket.emit('add player', myPlayer.playerData);

			GameLoop();

			SendMyDataToServer();

			console.log('my...