8 Replies Latest reply on May 12, 2010 7:25 PM by kevc0722

    Auto connect two peers


      Hi, i'm using stratus for one game and i want to know if there is any way that i can connect two peers automatically so they just go to the "Lobby" and there they wait for a peer to pop up, any help, any link everything helps


      Thanks and regards!

        • 1. Re: Auto connect two peers
          palkan_tula Level 1

          At least one of the peers have to "know" neerID of another to make a connection. But there is a one solution, using server-side code (e.g. php+SQL) .


          The 1st peer (let's call it "host") sends to the server some message "i'm here" with it's nearID (which is stored on the server) and receives server response if there is a the 2nd peer already here. If  it is so, the 1st simply connects to the 2nd and the 2nd connects to the 1st (using onPeerConnect Event).


          What to do if there is no the 2nd peer yet?


          When the 2nd peer sends it's "i'm here" message, the 1st is already there, so the 2nd recieves nearID of the 1st and connect to it.


          Without using server-side code it's impossible, I think.

          • 2. Re: Auto connect two peers
            jscamposcr Level 1

            Yes now i know that, after some research.... i will have to learn how to build that gate app so i can build my chat...., thanks!

            • 3. Re: Auto connect two peers
              kevc0722 Level 1

              So the server just stores IDs or does it point to a random ID that is already in the database as well?

              I guess my question is, what does the php/MySql do and what does the flash do?


              Right now it seems that


              php/MySQL - store connected ID

              flash - tries to find an random ID to connect to


              but I have no idea on how to make flash call for a random ID that's in the database.


              Any help is greatly appreciated, thanks in advance

              • 4. Re: Auto connect two peers
                palkan_tula Level 1

                "flash - tries to find an random ID to connect to"  — you mean something like chatroulette? Connect to Stratus -> get random peerId from database -> connect peers to each other?



                "but I have no idea on how to make flash call for a random ID that's in the database."


                so, you just have to make request to some "give_me_random_peer.php", which sends you all what you want. It is php/MySQL part, not Flash.

                • 5. Re: Auto connect two peers
                  abeall Level 3

                  I'm interested in this thread for pretty much the same reasons, but I may be one step ahead. Here's what I did for a basic 1v1 game experiement:


                  1. Create single PHP script which can take a request with two possible actions: "login" and "findOpponent"


                  2. Flash Player when it first boots up connects to Stratus, gets its nearID as usual, then sends its nearID and a user input friendly name to the PHP script. The PHP script writes those values to the sqlite database as a row and then returns a list of all rows (names + nearID) in the database.


                  3. Flash Player can then make a "findOpponent" request to the PHP script, which simply takes a friendly name -- either one pulled from the list returned previously, for instance the most recent one, or a manually entered one by the user. The PHP script queries the sqlite database to see if there's a row matching the given name, and if there is, it returns the nearID.


                  4. Now with a valid name and nearID of an oppononent, the Flash Player attempts to connect to it.


                  That all works okay, but there's one problem I can't conceptualize how to fix: how does the server know when a player is no longer available? Of course I could create an additional "logout" action for the PHP script which deletes the row associated with the user who is logging off, but of course most of the time a user would just close the window or browser so no logout would be called. Any peers they are connected to will know right away when a connection is dropped, but the server which is acting like the lobby/waiting room will have no idea. Therefor if a user logs on, never connects to an opponent, then closes the Flash Player, the server will never know and it will still be listed on the server as a waiting connection.


                  This isn't such a big problem if all the application needs to do is search for an available opponent, but it would eliminate the ability to have a reliable list of waiting opponents presented to the user since it would potentially be full of stale/broken connections.


                  Also, in terms of searching for a valid connection, I'm not even sure how to do that. When I try to connect to an old nearID (valid but no longer connected) I don't seem to get an error or NetStream status saying it failed to connect, it's just silent. Best I can imagine is to create a timeout?

                  • 6. Re: Auto connect two peers
                    jscamposcr Level 1

                    Well i am actually done with that project, i just created a DB and connected flash with PHP, when the peer gets online it ask for an opponent and if there is no one it adds the near peer id so when the next user gets online it will grab that peerid and erase it from the DB, my PHP code is no more than 25 lines long.


                    The time out idea is not bad, i would use the same.

                    • 7. Re: Auto connect two peers
                      abeall Level 3

                      So what if say:


                      1. A user connected

                      2. There's no other peer in db, so it adds this peer to db to wait for next guy

                      3. He gets impatient or his computer explodes, connection is lost

                      4. New guy comes, checks db and it gives peer id of previous ill-fated user


                      Would not that create a "false connection"? How do you deal with that?


                      And I guess you weren't going as far as to try to actually present a list of all "logged on" users at any point? That's where I'm even more stuck.

                      • 8. Re: Auto connect two peers
                        kevc0722 Level 1

                        My solution was using timeout as well.

                        I start a timeout counter when the app tries to connect to a far ID

                        if the far ID doesn't return an incomingStream within x seconds, it times out and jumps to another peer ID.

                        It still drags on the search process but at least it will try to search for another when the peer ID is idle.