all groups > flash actionscript > april 2004 >
You're in the

flash actionscript

group:

using Sockets to connect 2 different users.


using Sockets to connect 2 different users. sneakyimp
4/16/2004 11:29:04 PM
flash actionscript:
hi:

i'm trying to determine how i might make a 2 player game that people can play
over the internet. my server has PHP which supports sockets (or will as of
this weekend) but I'm curious about how to share data between different
sessions within PHP. i realize this question is more PHP than FLASH but
thought I'd ask in case anyone has experience with it.
Re: using Sockets to connect 2 different users. Pea
4/17/2004 3:14:53 AM
Hi,

I've got experience with sockets, and with PHP, but not with both :)

My understanding is that for PHP to act as a server, it has to start a process
(e.g. a listening socket) and that connection must stay open to accept socket
connections.

Perhaps this is done in a while(true) loop that never ends (or at least a
while(not_aborted) loop).

If this is correct then you shouldn't have any problem with 2 different users
sharing the same session because they should both be communicating with exactly
the same socket.

Psuedo:

open socket connection;
while (not aborted) {
if (socket has connected){
store the connection in a list;
}
foreach connected socket {
get data and process it;
if the data is an 'abort' message, then abort
}
}

What I'm not sure about is how to get this process started in the first
place....
I would think that flash could do a getVariables call to the server script,
which would start up. flash would never get a reply, but thats ok. Flash can
now connect using socket.

Probably have to deal with issues around the maximum execution time of a
script in PHP, and remeber to timeout the server script after x time of
non-activity.

Hope it helps,
Pea
Re: using Sockets to connect 2 different users. sneakyimp
4/17/2004 11:30:43 PM
Re: using Sockets to connect 2 different users. Pea
4/18/2004 4:03:05 AM
Hi,

I'm pretty sure that the answer is 1A. As long as your script deals well with
the different connections there should be no problems. As far as I am aware, a
single listening socket may have multiple 'connections' and you'll probably
find lots of code for storing each connection in an array, and reading and
sending data to each seperate connection.

The rest of it (session management, multiple games etc) I would suggest you
use the same script, and code your script to deal with these things. I suggest
a very simple scheme whereby the data packets are structured and can be used
for many games:

packet_type - game_code - player_id - payload

The packet type could be:
A - I want to start playing a game. I dont have an opponent.
B - I am playing a game
C - I am sending a command (stop server, disconnect me, etc)

game_code could be:
A - Game 1
B - Game 2

player_id could be a string of set length (e.g. 4 numerals) asigned by the PHP
script when a connection is made. This is sent with every subsequent request
to identify the player

payload is your game data (players position etc).

An example data packet:
AB0001ffa0

Note that the aim is to get the data packet as small as possible to reduce
network traffic. Another thing you have to look at is whether you can use UPD,
or whether communication has to be TCP. TCP is the usual method of sending
traffic over the internet. WHat happens with TCP is that a packet is sent and
then the socket WAITS untill it receives a response saying "yip, I got the
packet ok" and then it sends the next one. You can see that if there is a lot
of network traffic that the ball in pong would jerk around a lot and sit on the
spot sometimes.

A better method is to use UPD, which just sends the packet, and doesnt care if
it makes it or not. If the socket is ready to send another packet before a
response comes back its ok, because the socket isnt waiting.

Another thing thats good to do (are you still with me :)) is called
dead-reckoning. Without dead reckoning you might move your pong ball to the
location that is given in the data packet. This means that the ball sits at
this location until it gets the next packet. Dead reckoning means to continue
moving the ball where the game calculates it should be, and then update the
balls position as the packets come in.

Anyway, theres heaps of things that you should look into. Best to give me an
email: peter at pixelthis dot co dot nz

A very good resource on network gaming in general is gamasutra
(www.gamasutra.com) you'll need to sign up (free) to get access to the
articles, but they are written by the gaming guru's themselves :)

Cheers,
Pea
Re: using Sockets to connect 2 different users. Pea
4/18/2004 4:05:24 AM
Anyway, send me an email:
peter at pixelthis dot co dot nz
Because as you can see from my website
[L=pixelthis.co.nz]http://www.pixelthis.co.nz/[/L], flash games is what I do,
and it would be great to get something gong that we could both use :)
AddThis Social Bookmark Button