Source RCon - Getting chat with PHP

This is used for general discussion that is not necessarily server-related.
Post Reply
Riyuzakisan
New to forums
New to forums
Posts: 12
https://www.youtube.com/channel/UC40BgXanDqOYoVCYFDSTfHA
Joined: Wed Nov 13, 2013 10:42 am

Source RCon - Getting chat with PHP

Post by Riyuzakisan »

Hi,

I'm writing my own RCon application in PHP to send commands to my Garry's Mod server. I would ordinarily use the RCon provided by NFOServers on the Server Control panel, but having this feature on my own website makes matters a lot easier. I am already able to send RCon commands to the server and receive responses.

I looked at the page source on the Server Control page, and found that a request is being made every fraction of a second by javascript to get new log entries from the server (such as player chat) to display on the page in real-time - at least until the console times out from inactivity.

Now, obviously I can't see the server-side code used to make these server queries. So I'm wondering how I could replicate this on a remote website so that I could have the server chat updating in real time, or if it's even possible. I suppose I'm looking for a specific query/command to send to the server in order to get the log information back.

It seems sort of odd to be making a post of this nature here on this forum, but I thought I would have the most luck posting among other server owners/programmers who may have experience with this. Or perhaps some NFOServer folks could give me some pointers, if it's not a big trade secret :wink:
Riyuzakisan
New to forums
New to forums
Posts: 12
Joined: Wed Nov 13, 2013 10:42 am

Re: Source RCon - Getting chat with PHP

Post by Riyuzakisan »

So far, I've found that I need to add an IP:Port for the log to be sent to with the "logaddress_add" rcon command, and then to listen to that with a UDP connection to get the information. And finally, process it to get usable data and isolate the new log entries to display on-screen.
User avatar
Edge100x
Founder
Founder
Posts: 13112
Joined: Thu Apr 18, 2002 11:04 pm
Location: Seattle
Contact:

Re: Source RCon - Getting chat with PHP

Post by Edge100x »

You've got it, yep. We then put it into a database and pull from that database for web clients.
Riyuzakisan
New to forums
New to forums
Posts: 12
Joined: Wed Nov 13, 2013 10:42 am

Re: Source RCon - Getting chat with PHP

Post by Riyuzakisan »

Gotcha.
Thanks Edge.
Riyuzakisan
New to forums
New to forums
Posts: 12
Joined: Wed Nov 13, 2013 10:42 am

Re: Source RCon - Getting chat with PHP

Post by Riyuzakisan »

I figured out how to get this working, so I thought I'd make an update post if anyone else here happens to be looking for the same thing in the future.

I made a thread on the Facepunch forum, and posted my findings as well as the final PHP code:
Thread: Receive GMod Server log with PHP

I have this fully implemented into my site so that it stores the logs in a database, then uses JQuery on the RCon page to send a request for new logs; plus an inactivity timer so the console isn't constantly running.

I can provide further details if anyone is interested.
User avatar
hakkuo23
This is my homepage
This is my homepage
Posts: 88
Joined: Thu Aug 05, 2010 5:04 pm

Re: Source RCon - Getting chat with PHP

Post by hakkuo23 »

Edge100x wrote:You've got it, yep. We then put it into a database and pull from that database for web clients.
Question, wouldn't it be far more efficient to just make the client store it locally? I mean this sounds like one abused database lol, if EVERYTIME a response is received, it stores it into a database. How many rows is that, or do you truncate old responses?
Riyuzakisan
New to forums
New to forums
Posts: 12
Joined: Wed Nov 13, 2013 10:42 am

Re: Source RCon - Getting chat with PHP

Post by Riyuzakisan »

Facepunch seems to be having some trouble displaying the code in my post there, so I'll paste it here for convenience.

This is the code to create and bind to a UDP socket to listen for incoming data from a Source server:

Code: Select all

<?php
// IP:Port to send the logs to (web server)
$host="xxx.xxx.xxx.xxx";
$port = 10000;

// IP:Port the logs are being sent from (your server)
$remote_ip = 'xxx.xxx.xxx.xxx';
$remote_port = 27015;

// Create socket
if (!($socket = socket_create(AF_INET, SOCK_DGRAM, 0))){
	exit;
}

// Make reusable
if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)){
	exit;
}

// Bind the source address
if(!socket_bind($socket, $host , $port)){
	exit;
}

// Send first message - SRCDS needs to receive a message first before it starts sending
$msg = ""; // Message can be anything, it doesn't matter
$len = strlen($msg);
socket_sendto($socket, $msg, $len, 0, $remote_ip, $remote_port);

// Get database - my own convenience function
// You can disregard the bits of database code
if (!$db = get_database()){
	exit;
}

// Prepare query statement
$statement = $db->prepare("INSERT INTO server_logs (content) VALUES (:content)");

// Gather log data
while(1){
	$r = socket_recvfrom($socket, $buf, 512, 0);
	$buf = substr($buf, 7); // Remove weird characters at beginning
	$buf = str_replace(array("\0", "\r"), '', $buf); // Remove unwanted escape sequences
	
	// Log code to database (using PDO)
	$statement->execute(array(':content'=>$buf));
}
// Might want to implement your own failsafe system to break out of the above
// While loop to turn off logging

// Close PDO cursor
$statement->cursorClose();

// Close socket
socket_close($socket);
?>
The above code is slightly different than what I have on my site, because I edited it for posting on the forum. I haven't tested it, though it should work. I only say this as a disclaimer because I removed some code that pertained only to my site.

The database management code can be disregarded, and you can implement your own system for logging the data.

Also, there were the characters "ÿÿÿÿRL" at the beginning of each log entry. I'm not sure if this is part of the actual log from the server, or something else. Either way, it seemed to repeat consistently, so I thought it would be safe to trim that part out. If anyone here knows the cause of this, it would be nice to know.

Let me know if you spot any bugs in the code so I can correct them.
User avatar
Edge100x
Founder
Founder
Posts: 13112
Joined: Thu Apr 18, 2002 11:04 pm
Location: Seattle
Contact:

Re: Source RCon - Getting chat with PHP

Post by Edge100x »

Question, wouldn't it be far more efficient to just make the client store it locally? I mean this sounds like one abused database lol, if EVERYTIME a response is received, it stores it into a database. How many rows is that, or do you truncate old responses?
By storing it in a database, we allow multiple clients to use the tool simultaneously.

Yes, the database is kept in-memory and regularly trimmed.
User avatar
Edge100x
Founder
Founder
Posts: 13112
Joined: Thu Apr 18, 2002 11:04 pm
Location: Seattle
Contact:

Re: Source RCon - Getting chat with PHP

Post by Edge100x »

Also, there were the characters "ÿÿÿÿRL" at the beginning of each log entry. I'm not sure if this is part of the actual log from the server, or something else. Either way, it seemed to repeat consistently, so I thought it would be safe to trim that part out. If anyone here knows the cause of this, it would be nice to know.
Those are just part of the protocol and signal that it's a log entry. Similar characters are used in sending rcon commands, client-server communications, and so on.
Riyuzakisan
New to forums
New to forums
Posts: 12
Joined: Wed Nov 13, 2013 10:42 am

Re: Source RCon - Getting chat with PHP

Post by Riyuzakisan »

Edge100x wrote:Those are just part of the protocol and signal that it's a log entry. Similar characters are used in sending rcon commands, client-server communications, and so on.
That's sort of what I had imagined.


For anyone interested in evidence of this working, here's a screenshot of a Server Chat page:
http://i.imgur.com/xkfJi0V.png

It uses the same framework as the Console page. This is so the staff members of the server can log into the site (through Steam) and chat with players on the server.
happy57
New to forums
New to forums
Posts: 1
Joined: Wed Apr 16, 2014 3:07 pm

Re: Source RCon - Getting chat with PHP

Post by happy57 »

is this useable yet as i have a great need for something like this
jared2014
New to forums
New to forums
Posts: 5
Joined: Mon Oct 27, 2014 2:12 pm

Re: Source RCon - Getting chat with PHP

Post by jared2014 »

I was wondering if i could get some help please i am trying to create a remote RCon tool for my Co-Owner but i have not been able to make it work... So i was wondering if i could get the code to make it work.
Post Reply