Communication between 2 servers.
#1

Ok, hi all, i've been wondering how to make something that communicates between (my) 2 servers. I wanted to try to make a command like
Код:
/s(erver) <message>
But really how the f*ck should i do that ? (no, im not noob at least i have to use sscanf) I don't think there is a plugin for this or there is ?!
Help is appreciated !
Reply
#2

I don't know a lot about these things, but I think you could possibly try something with the socket plugin
Reply
#3

Not sure if you can but if is any way will be with mysql
You can save message to DB and read it from DB2.
Reply
#4

Sockets plugin should work. MySQL might work too, but will be slower as there's no event trigger. You'll have to keep polling for new messages (i.e. timers).
Reply
#5

Quote:
Originally Posted by TzAkS.
Посмотреть сообщение
Not sure if you can but if is any way will be with mysql
You can save message to DB and read it from DB2.
As suggested above, Sockets is the most ideal way to do this. Doing it with MySQL would be silly and far slower.
Reply
#6

If you have the database on localhost,where is running the server it will be fine.
And is not realy hard to do it
Reply
#7

You can also share the same MySQL database and pass commands through it, just make an table to send commands (with the fields ID, COMMAND [primary key, auto-increment], RESPONSE [varchar, default: NULL], EXECUTED [int], SERVER [int], TIME [int]) that will be used from the source server to the target server.

I don't know how can I explain it, because I really don't know anything about pawn script, but it is very simple... all you need is convert the php codes that I will post here to Pawn.

here is the function who sends an command to Server Y and wait until get an response:

PHP код:
<?php
$command 
"msgtoall Hello there, I am Server X!";
$waitTime 0//Seconds;
$time time();
@
mysql_query("INSERT INTO commands(COMMAND, SERVER, TIME) VALUES('{$command}', 2, {$time})");
while(
1) {
    
$query      = @mysql_query("SELECT * FROM commands WHERE COMMAND='{$str}' AND TIME={$time} ORDER BY TIME DESC LIMIT 0,1");
    
$resp       = @mysql_fetch_array($query);
            
    
$resp_exec  = (bool)$resp["EXECUTED"];
        
    if(
$resp_exec) { // If the command were executed successfuly
        
$return $resp["RESPONSE"];
        break;
    } else if(
$waitTime >= 5) { //If the request got no response for 5 seconds, stop the execution and return boolean false.
        
$return false;
        break;
    }
            
    ++
$waitTime;
    
sleep(1);
}
if(!
$return) {
    
SendAdminMessage("The command request to Server Y don't had any response."); //The function SendAdminMessage does not exists, I know T_T it is only a example.
}
?>
and in the Server Y game mode, you will set a timer to verify if "commands" table have rows with "EXECUTED" field containing value "0".

Код:
SELECT * FROM commands WHERE EXECUTED = 0
then you will fetch this query data, verifying the "COMMAND" field value.

PHP код:
<?php
$query 
= @mysql_query("SELECT * FROM commands WHERE EXECUTED = 0 AND SERVER = 2"); //Server 2 = Server Y, Server 1 = Server X. In Server X just change SERVER = 2 to SERVER = 1.
while($r = @mysql_fetch_array($query)) {
    
$id         $r["ID"];
    
$command    $r["COMMAND"];
    
    
$command    explode(" ",$command);
    
$cmd        $command[0];
    
    unset(
$command[0]);
    
    
$response   0;
    
    switch(
$cmd) {
        case 
"msgtoall":
            
$message implode(" "$command);
            if(
SendClientMessageToAll(0xFF0000$message)) { //Pawn function
                
$response 1;
            } 
        break;
        case 
"msgtoplayer":
            
$playerid   $command[1]; 
            unset(
$command[1]);
            
            
$message    implode(" "$command);
            if(
SendClientMessage($playerid0xFF0000$message)) {
                
$response 1;
            }
        break;
        
/** other cases **/
    
}
    
    
//After execute it, update the command row.
    
@mysql_query("UPDATE commands SET EXECUTED = 1, RESPONSE = {$response} WHERE ID = {$id}");
}
?>
You need these scripts in both servers, so you can send commands from Server X to Y and from Y to X, just changing the "SERVER" field in the query "INSERT".

It's logical that have security stuff missing, but I hope that you will understand the logic behind it.

I don't know about lag, I use this method to make an connection between my server and my website without lag, but this method can also works between two servers without lag as well.
Reply
#8

Quote:
Originally Posted by TzAkS.
Посмотреть сообщение
If you have the database on localhost,where is running the server it will be fine.
And is not realy hard to do it
Right, but as Vince said; There is no event, or trigger for data being put into a MySQL table, you'd have to constantly check, doing queries over and over, thus that's VERY inefficient and slow.

Where as sockets are EXACTLY what is needed and doesn't take much effort at all.
Reply
#9

That's not very inefficient, it's ideal for logging messages sent between servers. I'd use MySQL over sockets for a) stability and b) logging. There are advantages and disadvantages for both methods.
Reply
#10

Quote:
Originally Posted by Hiddos
Посмотреть сообщение
I don't know a lot about these things, but I think you could possibly try something with the socket plugin
Will try it.
Quote:
Originally Posted by TzAkS.
Посмотреть сообщение
If you have the database on localhost,where is running the server it will be fine.
And is not realy hard to do it
One db is localhost , the other not.

Quote:
Originally Posted by Mandrakke
Посмотреть сообщение
You can also share the same MySQL database and pass commands through it, just make an table to send commands (with the fields ID, COMMAND [primary key, auto-increment], RESPONSE [varchar, default: NULL], EXECUTED [int], SERVER [int], TIME [int]) that will be used from the source server to the target server.

I don't know how can I explain it, because I really don't know anything about pawn script, but it is very simple... all you need is convert the php codes that I will post here to Pawn.

here is the function who sends an command to Server Y and wait until get an response:

PHP код:
<?php
$command 
"msgtoall Hello there, I am Server X!";
$waitTime 0//Seconds;
$time time();
@
mysql_query("INSERT INTO commands(COMMAND, SERVER, TIME) VALUES('{$command}', 2, {$time})");
while(
1) {
    
$query      = @mysql_query("SELECT * FROM commands WHERE COMMAND='{$str}' AND TIME={$time} ORDER BY TIME DESC LIMIT 0,1");
    
$resp       = @mysql_fetch_array($query);
            
    
$resp_exec  = (bool)$resp["EXECUTED"];
        
    if(
$resp_exec) { // If the command were executed successfuly
        
$return $resp["RESPONSE"];
        break;
    } else if(
$waitTime >= 5) { //If the request got no response for 5 seconds, stop the execution and return boolean false.
        
$return false;
        break;
    }
            
    ++
$waitTime;
    
sleep(1);
}
if(!
$return) {
    
SendAdminMessage("The command request to Server Y don't had any response."); //The function SendAdminMessage does not exists, I know T_T it is only a example.
}
?>
and in the Server Y game mode, you will set a timer to verify if "commands" table have rows with "EXECUTED" field containing value "0".

Код:
SELECT * FROM commands WHERE EXECUTED = 0
then you will fetch this query data, verifying the "COMMAND" field value.

PHP код:
<?php
$query 
= @mysql_query("SELECT * FROM commands WHERE EXECUTED = 0 AND SERVER = 2"); //Server 2 = Server Y, Server 1 = Server X. In Server X just change SERVER = 2 to SERVER = 1.
while($r = @mysql_fetch_array($query)) {
    
$id         $r["ID"];
    
$command    $r["COMMAND"];
    
    
$command    explode(" ",$command);
    
$cmd        $command[0];
    
    unset(
$command[0]);
    
    
$response   0;
    
    switch(
$cmd) {
        case 
"msgtoall":
            
$message implode(" "$command);
            if(
SendClientMessageToAll(0xFF0000$message)) { //Pawn function
                
$response 1;
            } 
        break;
        case 
"msgtoplayer":
            
$playerid   $command[1]; 
            unset(
$command[1]);
            
            
$message    implode(" "$command);
            if(
SendClientMessage($playerid0xFF0000$message)) {
                
$response 1;
            }
        break;
        
/** other cases **/
    
}
    
    
//After execute it, update the command row.
    
@mysql_query("UPDATE commands SET EXECUTED = 1, RESPONSE = {$response} WHERE ID = {$id}");
}
?>
You need these scripts in both servers, so you can send commands from Server X to Y and from Y to X, just changing the "SERVER" field in the query "INSERT".

It's logical that have security stuff missing, but I hope that you will understand the logic behind it.

I don't know about lag, I use this method to make an connection between my server and my website without lag, but this method can also works between two servers without lag as well.
You sure that works ? xD
Will test it also
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)