Author Topic: HTTP GET requests  (Read 1509 times)

Bradamante3D

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 79
    • View Profile
HTTP GET requests
« on: April 09, 2016, 05:31:30 AM »
In the feature description for TNet 3.0 it says:

Quote
- NEW: TNet is now able to respond to a simple HTTP browser GET request. Simply connect to your server via http://127.0.0.1:5127/

I am not sure I understand. This can't work without some webserver like Apache, right? If so, I find that feature point a bit misleading. And I don't mean that as an accusation, I just find it a bit unclear. What exactly do I have to do to use this feature?
#301224014, #301432336, #302399130

cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: HTTP GET requests
« Reply #1 on: April 09, 2016, 07:14:22 AM »
Apache is *A* webserver but it is not *THE* webserver. Think of this feature as making TNet a very, very, very stripped down version of Apache. This feature makes TNet its own webserver (though very limited, don't expect to host websites with it :p)

I'm no web programmer and have very little experience in this area, but you could do something like this:
test.php:
http://pastebin.com/RDDGHPdk (had to upload to pastebin as html tags aren't allowed on this forum, even when enclosed in bbc code tags)

This would create a button that - when clicked - will send the "Shutdown=1" message to the TNet instance running on 127.0.0.1:5127.
You would then have to write some code on TNet's side to handle this message. I don't have TNet 3 so I can't provide an example for that, but maybe someone else can.

You can either install php (w/ curl) on your own computer and run test.php from your web browser, or you can upload test.php to a web host (that has php and curl support already) and use it from any computer.

This featue is useful because it enables you to build a web interface to remotely administrate your TNet server. It could serve many other uses as well if you get creative with it, such as having a website w/ a live leaderboard or a live feed of what's happening in your game.
Also, System.Net isn't available on mobile devices, right? So this feature could make TNet usable on mobile devices assuming it doesn't use the System.Net namespace. Also, you'd have to flesh it out a bit to add support for POST requests as GET isn't suitable for sending a lot of data, but still, it'd be a big step forward.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: HTTP GET requests
« Reply #2 on: April 09, 2016, 08:56:09 AM »
While you certainly can expand the server to handle specific GET requests, right now it simply treats all GET requests as one, and responds with an HTML page. Try it locally -- host a server with -http flag and connect to it in your favorite web browser by using http://127.0.0.1:5127 syntax in the address bar. You will see something like this:
  1. Name: Tasharen
  2. Clients: 9
  3. 1 Rico
  4. 2 Zomx
  5. 3 Ninnin Phoenix
  6. 4 Guest
  7. 5 Guest
  8. 6 Plante
The number of clients includes all active connections (which includes the HTTP GET request itself). The list of players only has actual verified players. You can basically use this response in a PHP script to display a list of players on your server on your website.

Bradamante3D

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 79
    • View Profile
Re: HTTP GET requests
« Reply #3 on: April 09, 2016, 09:00:33 AM »
Oh I know what this is useful for. In the early 2000s me and some friends were running a Quake 3 server and later a PVPGN server and they had this functionality too. We would then go and code a bit of a website that showed server status, player count, games running etc.

I just wondered about the requirements and how to use it. How can this work by installing PHP but not Apache or some other webserver? How would I know which commands TNet can respond to? It's not like I can do a TNServer.exe -help and see a list.
#301224014, #301432336, #302399130

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: HTTP GET requests
« Reply #4 on: April 09, 2016, 09:02:42 AM »
You don't need to install anything. The TNet's server acts as a very basic web server in this case. It basically gives a different response based on whether you are connecting to it with TNet, or with a web browser, that's all.

Bradamante3D

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 79
    • View Profile
Re: HTTP GET requests
« Reply #5 on: April 09, 2016, 09:16:50 AM »
Hm I think I understand. When I do this with my local Raspberry setup I get a:

  1. Name: TNetServerTest
  2. Clients: 2
  3. 1 Guest
  4. 2 Editor

How do I edit this output? Is there a template file? Or is it hardcoded? Is there a documentation somewhere what commands I can use?
#301224014, #301432336, #302399130

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: HTTP GET requests
« Reply #6 on: April 09, 2016, 09:22:36 AM »
Just search for "case Packet.RequestHTTPGet". There are two locations. Line 1923 of TNGameServer.cs and line 387 of TNTcpLobbyServer.cs.

xandeck

  • Jr. Member
  • **
  • Thank You
  • -Given: 1
  • -Receive: 3
  • Posts: 54
    • View Profile
Re: HTTP GET requests
« Reply #7 on: April 12, 2016, 07:23:26 AM »
This sounds really good.
So, can I connect also to remote servers, not only local?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: HTTP GET requests
« Reply #8 on: April 13, 2016, 07:43:50 AM »
If those remote servers were started with -http flag, yes.