Postanowiłem uruchomić moją grę przeglądarkową.
Po kilku latach postawiłem sobie na localhost w xampp cały silnik gry ale pozostał ostatni element do uruchomienia- ale pokonał mnie

Aby płynnie wymieniać dane między serwerem a przeglądarką i miedzy graczami zastosowano node.js
Nie jestem autorem tego kodu a kontakt z autorem się urwał

Wkleję przykładowe kody- jeśli chcesz pomóc i masz wiedzę nt. node.js- napisz do mnie.
Mój plik app.js wygląda następująco:
var express = require('express'); var app = express(); var server = require('http').createServer(app); var io = require('socket.io')(server); server.listen(process.env.PORT || 3000); console.log('Server is running '); var mysql = require('mysql'); var fs = require('fs'); /* Load modules */ var validator = require('validator'); var serverKey = 'sdfgsdfgs7df564erwfsad'; var db_config = { host: 'localhost', user: 'root', password: '', database: 'wichry_wojny' }; var connection; function handleDisconnect() { connection = mysql.createConnection(db_config); // Recreate the connection, since // the old one cannot be reused. connection.connect(function(err) { // The server is either down if(err) { // or restarting (takes a while sometimes). console.log('error when connecting to db:', err); setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect, } // to avoid a hot loop, and to allow our node script to }); // process asynchronous requests in the meantime. // If you're also serving http, display a 503 error. connection.on('error', function(err) { console.log('db error', err); if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually handleDisconnect(); // lost due to either server restart, or a } else { // connnection idle timeout (the wait_timeout throw err; // server variable configures this) } }); } handleDisconnect(); app.io.route('ready', function(req) { connection.query('SELECT s.id, s.access, s.worldID, p.nation, p.id AS uid FROM sessions AS s LEFT JOIN players AS p ON (p.accountID=s.account AND p.worldID=s.worldID) WHERE s.session_key=\''+validator.escape(req.data.ident)+'\' AND s.access!=\'GUEST\' LIMIT 1', function(err, rows) { if (err) { console.error('error connecting (sid '+validator.escape(req.data.ident)+'): ' + err.stack); return; } if (rows.length !== 0) { if (rows[0].length === 0) return; var userData = rows[0]; // join main world chat room console.log('Gracz dolaczyl do gry - swiat:'+userData.worldID+', nacja:'+userData.nation+', uid:'+userData.uid); req.io.join('worldchat'+userData.worldID); // join nation chat req.io.join('worldchat'+userData.worldID+'nation'+userData.nation); // join private chat req.io.join('worldchat'+userData.worldID+'private'+userData.uid); // join world map data req.io.join('worldmap'+userData.worldID+'nation'+userData.nation); //app.io.room(req.data.room).broadcast('announce', { // message: 'Nowy czatowicz ('+rows[0].access+') na kanale: ' + validator.escape(req.data.room) + '. ' //}); } else { req.io.emit('noconn'); } }); });
Mam również np. metodę
public function updateUnitData($intUnitID, $updateType, $zmienne) { $objHelper = init::getFactory() -> getService('helper'); $fieldAdd = false; ... operacje na bazie danych itp. $objHelper -> sendReqToNodeJS($arrayData, ADRES . ':3000/game'); // ADRES to 'http://localhost:8020'; jako vhost xampp $arrUnitData = null; }
public function sendReqToNodeJS($arrayData, $urlData = ADRES .'/message') { // set password $arrayData['password'] = 'sdfgsdfgs7df564erwfsad'; //if (!isset($_SESSION['cookieFile'])) $_SESSION['cookieFile'] = tempnam (BASE_DIR."/tmp", "connect.sid"); $ch = curl_init($urlData); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_HEADER, TRUE); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_NOBODY, FALSE); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($arrayData)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2); 'Content-Type: application/json', ); $res = curl_exec($ch); //if (substr($res, 0, 12) != 'HTTP/1.1 200') //{ // debug::saveLog(get_class($this).': '.date('H:i:s d-m-Y').' : Linia 50 - nodeJS zwrócił stronę błędu '.var_export($arrayData, true)); //} }
A w pliku html mam takie elementy:
<script type="text/javascript"> io = io.connect('{{ constant("ADRES") }}:3000', { 'force new connection': true, transports: ['xhr-polling'] }); // 'websocket', function socketConnect(mapIdent) { // Send the ready event. io.emit('ready', {'ident': mapIdent}); io.on('noconn', function(data) { jAlert('Nie udało się połączyć z serwerem mapy. Odśwież stronę.'); }); } io.on('message', function(data) { if (readCookie('chatSounds') && data.info.playerID != {{ player.playerID }}) soundAPI.init(['maszyna.mp3'], false); if($('#chatbox').find('#sb_'+data.info.chanType).length === 0) { $('#chatTab_'+data.info.chanType+' span').html('(N)'); } if ($('#radio').css('display') == 'none' && readCookie('chatSounds') === null) { $('#przyciskRadio').html('<span class="blink">Ra[N]</span>'); } $('#sb_'+data.info.chanType+' ul').animate({ scrollTop : $('#sb_'+data.info.chanType+' ul').height() * 20 }, 'slow'); });
Niestety, mimo uruchomienia pliku app.js ( widzę info że serwer nasłuchuje portu 3000 i mapa się uruchamia ) ale wykonanie ruchu jednostką zmienia dane w bazie danych- ale brak aktualizacji rekordów na mapce- po przeładowaniu mapy jednostka zmienia lokalizację na mapie.
Potrzebuję pomocy w uruchomieniu tego node.js
