GetServerIp: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Switched to reliable IP provider domain)
No edit summary
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Useful_Function}}
<lowercasetitle/>
__NOTOC__
__NOTOC__
 
{{Client function}}
This function gets the server ip.
{{New feature/item|3.0161|1.6.0|22890|This function returns the IP of the server the client is currently connected to.}}
{{Note|On server-side there is the [[getServerIpFromMasterServer]] function, which might return the server IP under certain conditions.</br>You should consider using a third-party service via [[fetchServerIp]] ''(not an MTA function)'' if the previous function is unreliable for you.}}


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">[string]string getServerIp ( )</syntaxhighlight>
==Code==
<section name="Server" class="server" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
SERVER_IP = "127.0.0.1"
string getServerIp ( [ bool includePort = false ] )
</syntaxhighlight>


function getServerIp()
===Optional Arguments===
    return SERVER_IP
*'''includePort:''' If set to ''true'', the string will include the port of the server at the end of the string, after a colon (:) character.
end


fetchRemote("http://checkip.dyndns.com/",
===Returns===
    function (response)
A string containing the remote address of the server the client is currently connected to.
        if response ~= "ERROR" then
            SERVER_IP = response:match("<body>Current IP Address: (.-)</body>") or "127.0.0.1"
        end
    end
)
</syntaxhighlight>
</section>


==Example==
==Example==  
<section name="Server" class="server" show="true">
This example creates a console command that outputs the server's IP to the chatbox.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
outputServerLog("Public server IP: " .. getServerIp())
function outputServerIp()
    outputChatBox("You are currently connected to ".. getServerIp(true))
end
 
addCommandHandler("serverIp", outputServerIp)
</syntaxhighlight>
</syntaxhighlight>
</section>
==Credits==
* '''Author:''' MJNONFIK
* '''Edited by:''' Necktrox


==See Also==
==See Also==
{{Useful_Functions}}
{{Utility_functions|client}}

Latest revision as of 20:07, 8 January 2025

ADDED/UPDATED IN VERSION 1.6.0 r22890:
This function returns the IP of the server the client is currently connected to.
[[{{{image}}}|link=|]] Note: On server-side there is the getServerIpFromMasterServer function, which might return the server IP under certain conditions.
You should consider using a third-party service via fetchServerIp (not an MTA function) if the previous function is unreliable for you.

Syntax

string getServerIp ( [ bool includePort = false ] )

Optional Arguments

  • includePort: If set to true, the string will include the port of the server at the end of the string, after a colon (:) character.

Returns

A string containing the remote address of the server the client is currently connected to.

Example

This example creates a console command that outputs the server's IP to the chatbox.

function outputServerIp()
    outputChatBox("You are currently connected to ".. getServerIp(true))
end

addCommandHandler("serverIp", outputServerIp)

See Also