It is currently Mon May 29, 2017 1:07 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: [SOLVED] Respond to an HTTP request (noob question!)
PostPosted: Fri Feb 17, 2017 4:44 pm 
Offline
Enthusiast
Enthusiast

Joined: Wed Sep 18, 2013 11:54 am
Posts: 295
Location: France
Hi,

I'm pretty new to web development, and I'm playing with various stuff right now, but what I'd like to do and understand is how to make a communication between a client and a PureBasic server.

The idea is to send a jQuery get request with the following code :

Code:
$("#get-button").on("click", function(event) {
    $.get("http://localhost:6832", function(data) {
        console.log(data);
    });
});


I receive the connection (from the PureBasic example NetworkServer.pb), but I don't know what to answer to make it to the callback and print "data" into the console (I naively thought that SendNetworkString(ClientID) would be enough...). Besides, it would be good to send something else than a HTTP header from JS.


Last edited by Joubarbe on Sat Feb 18, 2017 9:41 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Respond to an HTTP request (noob question!)
PostPosted: Sat Feb 18, 2017 3:44 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Mon Oct 26, 2015 2:55 am
Posts: 719
Location: Ukraine
Hi. It looks like you need HTTP server, not just a TCP/UDP server (which is NetworkServer.pb).

I don't remember such HTTP server examples on PB and didn't yet make attempts to make own, but here is some example of server responce for GET query:

Code:
HTTP/1.1 200 OK
Content-Type: text/html
Date: Sat, 18 Feb 2017 02:36:59 GMT
Expires: Sat, 25 Feb 2017 02:36:59 GMT
Last-Modified: Fri, 09 Aug 2033 23:54:35 GMT
Server: YourHTTPSERVER
Content-Length: 95

HERE IS STRING TO PRINT IN BROWSER CONSOLE, SET CORRECT Content-Length TO A SIZE OF THIS STRING


You should send data like this using SendNetworkString(). #CRLF sequence used as new line separator if I'm not wrong.

_________________
... but there is no end to creation


Top
 Profile  
Reply with quote  
 Post subject: Re: Respond to an HTTP request (noob question!)
PostPosted: Sat Feb 18, 2017 9:03 am 
Offline
Enthusiast
Enthusiast

Joined: Wed Sep 18, 2013 11:54 am
Posts: 295
Location: France
Thanks Lunasole, your message makes me searched in the right direction and I came with that solution:

Code:
Define response.s = ~"HTTP/1.1 200 OK\r\nCache-Control: no-cache, private\r\nContent-Length: 95\r\nDate: Mon, 24 Nov 2014 10:21:21 GMT\r\nAccess-Control-Allow-Origin: *\r\nConnection: close\r\n\r\n" +
                    "HERE IS STRING To PRINT IN BROWSER CONSOLE, SET CORRECT Content-Length To A SIZE OF THIS STRING"


\r\n\r\n at the end is important, as well as Access-Control-Allow-Origin. Connection: close is optional though (I assume it's better to close the connection if the client has nothing more to say...).
Date should be properly pre-formatted somewhere.

It's quite fun to manipulate low level requests ; I shoud learn node.JS :)

Now I just have to figure out how to send parameters to the server (aka understand the $.ajax() function).

EDIT: which is:

Code:
$.ajax("http://localhost:6832", { data: "THIS_IS_DATA_FROM_CLIENT" }).done(function (data) {
    console.log(data);
});


And in PB, something like that (messy but it works ^^):

Code:
ReceiveNetworkData(ClientID, *Buffer, 1000)
               
Define clientRequest.s = PeekS(*Buffer, -1, #PB_UTF8) ; HTTP request header
Define clientData.s = StringField(StringField(clientRequest, 2, "/?"), 1, " ")
               
MessageRequester("Client data", clientData) ; parameters from client
SendNetworkString(ClientID, response) ; parameters to client (response)


Top
 Profile  
Reply with quote  
 Post subject: Re: [SOLVED] Respond to an HTTP request (noob question!)
PostPosted: Sat Feb 18, 2017 11:04 am 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 3498
Location: Germany
Hi,

you should always send your string as UTF8.

Bernd


Top
 Profile  
Reply with quote  
 Post subject: Re: [SOLVED] Respond to an HTTP request (noob question!)
PostPosted: Sat Feb 18, 2017 12:10 pm 
Offline
Enthusiast
Enthusiast

Joined: Wed Sep 18, 2013 11:54 am
Posts: 295
Location: France
You mean adding "Content-Type: text/html; charset=utf-8" to the header?


Top
 Profile  
Reply with quote  
 Post subject: Re: [SOLVED] Respond to an HTTP request (noob question!)
PostPosted: Sat Feb 18, 2017 12:53 pm 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 3498
Location: Germany
Yes, and...
Code:
SendNetworkString(..., ..., #PB_UTF8)

But mention your ContentLength.
Bernd


Top
 Profile  
Reply with quote  
 Post subject: Re: Respond to an HTTP request (noob question!)
PostPosted: Sat Feb 18, 2017 4:11 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Jun 02, 2003 9:16 am
Posts: 2032
Location: Germany
Lunasole wrote:
Hi. It looks like you need HTTP server, not just a TCP/UDP server (which is NetworkServer.pb).

I don't remember such HTTP server examples on PB and didn't yet make attempts to make own
There is the Atomic Webserver.

_________________
bye,
Daniel


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: Yahoo [Bot] and 14 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye