# XMLSocket : Live game

**URL:** <https://forum.kirupa.com/t/xmlsocket-live-game/120187>\
**Category:** Uncategorized\
**Created:** [August 21, 2004, 6:21pm UTC](https://forum.kirupa.com/t/xmlsocket-live-game/120187 "2004-08-21T18:21:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![louismullie](https://avatars.discourse-cdn.com/v4/letter/l/439d5e/32.png) [@louismullie](https://forum.kirupa.com/u/louismullie)\
**Post date:** [August 21, 2004, 6:21pm UTC](https://forum.kirupa.com/t/xmlsocket-live-game/120187/1 "2004-08-21T18:21:33Z")

</div>

Hey all, me and some other buds are creating a game where we need to have multiple players in ‘real time’. I have tested the solution of using a database/text file to handle the player positions, but it used huge amounts of CPU w/ only 2 players. So I wrote a socket server in PHP, and now I need help creating the AS part of it 🙂

I already wrote a bunch of accessors, I would like you to tell me if this is the best way to do it, as I am a total AS n00b.

```auto
/* Handle the receiving of player positions */
XMLSocket.prototype.onData = function (PlayerList)
{
	var i = 0;
	var CurrentUsername = "";
	var PlayerArray = PlayerList.split("|");
	for(Container in PlayerArray)
	{
		if (i == 0)
		{
			CurrentUsername = Container;
			i = i + 1;
		} 
		if (i == 1)
		{
			//assign coordinates to player with CurrentUsername;
			i = 0;
			CurrentUsername = "";
		} 
	} 
} 

/* Initialize socket connection */
_global.InitSocket = function(Username)
{
	var IP = "xxx.xxx.xxx.xxx"; // IP address of socket server
	var Port = "xxxx"; // Port of socket server to listen on
	_global.socket = new XMLSocket(); // Create socket object
	_global.socket.connect(IP, Port); // Connect to socket
	_global.socket.send(Username);	// Send username
	_global.socket.send("0,0"); // Initial position
} 

/* Close socket connection */
_global.CloseSocket = function()
{
	_global.socket.close();
} 

/* Send new player position to socket */
_global.SendPosition = function(Position)
{
	_global.socket.send(Position);
} 
InitSocket("PLAYERUSERNAME");

```

---

<div class="post-metadata">

**Author:** ![deQue](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@deQue](https://forum.kirupa.com/u/deQue)\
**Post date:** [August 21, 2004, 8:51pm UTC](https://forum.kirupa.com/t/xmlsocket-live-game/120187/2 "2004-08-21T20:51:26Z")

</div>

Well, all and all, it seems quite well thought out.  
Only thing you should make a habit of is strict typing of variables:

```php
/* Handle the receiving of player positions */
XMLSocket.prototype.onData = function (playersList) {
	var playersArray:Array = playersList.split("|");
	for(var i=0; i<playerArrays.length; i++) {
			var currentUsername = playersArray;
			// Not quite sure what your code was doing here, but here's my grasp of it...
			with (CurrentUsername) {
				//assign coordinates to player with CurrentUsername.
			}
		} 
	} 
}

```

As you can see, I’ve also changed your **_for_** loop to a more conventional way.  
The reason for this is because the **_for … in_** loop goes from the “end” of the array, to the begninning, while this one goes from 1 - up, and is a proper to use a loop with an array. the **_for … in_** is usually used for objects that you have no way of telling how many objects there are, or what’s their identifiers are.

[size=1] **A note of Variable Typing:** When you type variables it enables the flash compiler to find and identify errors better. In your specific case it doesn’t make that much of a difference, but in more complex applications this makes a world of difference![/size]  
[size=1]\* Also, typing enables the AS panel to provide you with relative drop-downs in some cases, which speeds up coding.[/size]
