I’m currently programming a game with some other peeps, and were using a socket server to keep track of the positions of the players. Ill outline the server-client communication :
- Client connects
- Server sends “000\0”;
- Client sends Username + “\0”
- Client sends Position + “\0”
- Server broacasts new list of players + positions
in the format :
Username:::Position|Username:::Position etc… \0
The server side part is working, as it works perfectlty when we telnet to it.
But when we try in flash with the following code, step 1 works, then the server receives the username, but we cant get it to receive any data. The server works in a way that it wont receive data it it isnt delimited by
. Heres the AS were using :
/* Handle the receiving of player positions */
XMLSocket.prototype.onData = function (PlayerList){
if(PlayerList == "000") {
return;
}
var i = 0;
var CurrentUsername = "";
var CurrentInfo;
var PlayerArray = PlayerList.split("|");
for(i=0; i<PlayerArray.length; i++)
{
CurrentInfo = PlayerArray*;
CurrentInfo = CurrentInfo.split(":::");
// CurrentInfo[0] is the username, CurrenInfo[1] is the position for that user
}
}
/* Initialize socket connection */
_global.InitSocket = function(Username){
var IP = "[www.billybussey.com](http://www.billybussey.com/)"; // Ip to listen on
var Port = 7777; // Port to listen on
_global.socket = new XMLSocket(); // Create socket object
_global.socket.connect(IP, Port); // Connect to socket
_global.socket.send(Username + newline); // Send username
_global.socket.send("0,0" + newline); // 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);
}
_global.InitSocket("Meagain");
I am suspecting that the sending of the username is not terminated, because on my server console i can see that it sent a username, but i think it hangs there because when we telnet afterwards, we cannot see this username in the list of positions.