dimanche 14 février 2016

Third Party server with UE4 : part 1 : nodejs

I have discovered that ue4 is very good for certain purpose, but not good for others.
One of the point is that if you want to manage persistance, or if you want to have information you can dispatch to all players, you need to use a third party server for this (for chat purpose too for example, because you must know all players who are playing to the game).

I ve found piece of code to write a nodejs server. For the moment, it is the easiest way (if you know javascript of course) to write an external third party server. It is light, easy to write, and enough for my purpose.
This server will:
-- receive connection
-- receive TCP message
   -- receive login message
       -- check if the login/password are ok in database
       -- send a json to the sender by TCP message



var net = require('net');
var mysql = require('mysql');

var mySqlClient = mysql.createConnection({
  host     : "*****",
  user     : "****",
  password : "********",
  database : "*******"
});


// Keep track of the chat clients
var clients = [];

// Start a TCP Server
net.createServer(function (socket) {
   
  // Identify this client
  socket.name = socket.remoteAddress + ":" + socket.remotePort

  // Put this new client in the list
  clients.push(socket);

  // Send a nice welcome message and announce
  //~ socket.write("Welcome " + socket.name + "\n");
  broadcast(socket.name + " joined the chat\n", socket);

  // Handle incoming messages from clients.
  socket.on('data', function (data) {
    //broadcast(socket.name + "> " + data, socket);
      try{
        var val = JSON.parse(data);
        if (val.code == "1"){
            login(socket,val);
        }
      }catch(err){
            console.log("data received not in JSON format : "  + data  + "/////" + err);
      }
  });
    socket.on('error',function(){
        console.log('socket reset');
        clients.splice(clients.indexOf(socket), 1);
    });
  // Remove the client from the list when it leaves
  socket.on('end', function () {
    clients.splice(clients.indexOf(socket), 1);
    broadcast(socket.name + " left the chat.\n");
  });
 
  function login(sender,jsonObj){
      //~ console.log(jsonObj);
      var selectQuery = "SELECT * FROM table where name ='" + jsonObj.login + "' and passwd = '" + jsonObj.password +"'";
       var status=0;
        var sqlQuery = mySqlClient.query(selectQuery);
        sqlQuery.on("result", function(row) {

          status=1;
        });
       
        sqlQuery.on("end", function() {
            //~ if (status){
          //~ mySqlClient.end();
            //~ }
          sender.write('{"code":"1","status":"' + status + '"}');
        });
       
        sqlQuery.on("error", function(error) {
          console.log(error);
            sender.write('{"code":"1","status":"-1"}');
        });

  }
 
  // Send a message to all clients
  function broadcast(message, sender) {
    clients.forEach(function (client) {
      // Don't want to send it to sender
      if (client === sender) return;
      client.write(message);
    });
    // Log it to the server output too
    process.stdout.write(message)
  }

}).listen(5000);

// Put a friendly message on the terminal of the server.
console.log("Chat server running at port 5000\n");







 

Aucun commentaire:

Enregistrer un commentaire