Skip to content

Gitting data by FastCGI protocol

Roman edited this page Jan 12, 2017 · 3 revisions

Gitting data by FastCGI protocol

Add deps to rebar.config

{sgi, ".*", {git, "git://github.com/astronin/sgi", {tag, "master"}}}

Add initialization of FastCGI protocol

This function is need to call for start ETS tables and check several variables from a server. You can call the function in main module of your project.

sgi_fcgi:init_fcgi()

Getting data

Add following code into the index:main() function like in exemple. You will send http request collected from the Cowboy and will get binary data from a server. For the *PHP it will be like conventional request from any other server. The Ajax request will be handled also here.

{Ret, Status, Headers} = sgi_n2o_fcgi_handler:send(),
  • Ret - result in binary,
  • Status - http status of respones
  • Headers - http headers of respones

WebSocket

Add new event in your index file for handle the WebSockets (index-ws.erl). This event receive emplimentation of HTTP from the JS and return to the JS a data, status and http headers. We emulate a Ajax request to the *PHP using the WebSocket. The *PHP will see conventional a Ajax request.

event(#http{url = _Url, method = _Method, body = _Body} = Http) ->
    {Ret, Status, Headers} = sgi_n2o_fcgi_handler:send(Http),
    wf:wire("http.back('"++wf:to_list(js_escape(Ret))++"', "++wf:to_list(Status)++", "++wf:to_list(jsone:encode(Headers))++")");

See examples for more details.

In JS

$conn.onconnect = function() {
    http.send('/')
        .done(function(data, status, headers) {})
        .fail(function(data, status, headers) {});
};

You can use object of settings in param for function send()

var obj = {
    url:'/',
    method:'POST',
    body:'Some Post Data',
    headers:{},
    returnType:'html', // Can be 'html' or 'json'
}
http.send(obj);

*PHP - it can be any other language working by FastCGI protocol

Clone this wiki locally