Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by Joncom

I'm using socket.io to allow players in my game see each other walk around. I currently have things set up so that when players join the game (depending what their username is) a particular "start position" is given to them. And that is where they start in the map.

The problem is this: so far I've had to input those positions into the database myself. I want the game to periodically save the players positions back into the database. I have a PHP file already coded and on standby for just the task. It can update the database simply by access it like so:
update.php?user=joncom&x=16&y=64&facing=left


My question is: how can I call this URL from my socket server, because it's not a browser, right? So AJAX doesn't really work the same, does it?

In any case, I tried to do AJAX regardless. Here's the a snippet from server.js, my socket application:
socket.on('receiveMove', function (currX, currY, direction, client) {
        socket.broadcast.emit('moveOtherPlayer', currX, currY, direction, client);
        for(var i in players)
        {
            if(players[i].name==client)
            {
                var newX = currX;
                var newY = currY;
                switch(direction)
                {
                    
                    case 'left':
                        newX = currX - 16; // !! magic numbers, not cool !!
                        break;
                    case 'right':
                        newX = currX + 16; // !! magic numbers, not cool !!
                        break;
                    case 'up':
                        newY = currY - 16; // !! magic numbers, not cool !!
                        break;
                    case 'down':
                        newY = currY + 16; // !! magic numbers, not cool !!
                        break;
                };
                players[i].pos.x = newX;
                players[i].pos.y = newY;
                players[i].facing = direction;
                
                // update positions on database
                var url = "update.php?user=" + client +
                          "&x=" + newX + "&y=" + newY +
                          "&facing=" + direction;
                var xmlhttp;
                if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp = new XMLHttpRequest();
                }
                else { // code for IE6, IE5
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange = function() {
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                        document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
                    }
                }
                xmlhttp.open("GET", url, true);
                xmlhttp.send();
                
                break; // no need to search for other players with same name
            }
        }
    });

If anybody has experience with sockets and synchronizing the information that they have into a database, help! :)

PS - Please excuse the typos if any... it's late.

1 decade ago by Joncom

Still haven't been able to transfer data stored by my socket server app into a database. General ideas, without code welcome! :p

1 decade ago by blingum

If you have remote access to the database or if the database is running on the same box, I'd be tempted to use some of the node MySQL drivers and connect directly to the database to store the data. I think that might be the fastest option, since you don't have to put the data through a pass-through PHP page.

I might also be missing something in your code, but it looks like you're pointing directly to update.php and not something like http://www.example.com/update.php. I don't believe node can resolve to just update.php and know it's pointing to the same server. You'll probably want the full URL to the update page.

I also would just POST to the update page instead of doing an AJAX request. The AJAX request might still work though. I've not tried it.
Page 1 of 1
« first « previous next › last »