I tried this now, but it's alway giving me an error, no matter what I try 
to download:

char _lastResponse[1024] = { '\0' };
int _wgetLock = 0;
bool _success = false;

void _net_ems_onload(void * arg, void * data, int size)
{
if(size > 1023) size = 1023;

RLog("EMS in onload callback, got response, size = %d", size);

memcpy(_lastResponse, data, size);
_lastResponse[size] = '\0';
_success = true;
_wgetLock = 0;
}

void _net_ems_onerror(void * arg)
{
RLog("EMS onerror callback :(");
_success = false;
_wgetLock = 0;
}

bool net_httpGet(const char * page,
const char * key1, const char * val1,
const char * key2, const char * val2,
const char * key3, const char * val3,
char * response)
{
if(page == NULL) return false;

RLog("EMS httpGet entry, target = \"%s\"", page);

//while(_wgetLock) { cu_sleep(100); }

RLog("EMS Lock passed, pausing main loop");

_wgetLock = 1;
emscripten_pause_main_loop();

char request[1024] = { 0 };
memset(_lastResponse, 0, 1024);

strcpy(request, page);

RLog("EMS Preparing request");

if(key1 != NULL && key2 != NULL)
{
strcat(request, "?");
strcat(request, key1);
strcat(request, "=");
strcat(request, val1);

if(key2 != NULL && val2 != NULL)
{
strcat(request, "&");
strcat(request, key2);
strcat(request, "=");
strcat(request, val2);

if(key3 != NULL && val3 != NULL)
{
strcat(request, "&");
strcat(request, key3);
strcat(request, "=");
strcat(request, val3);
}
}
}

//strcpy(request, "http://www.google.com";);

_success = false;

RLog("EMS Full request = \"%s\"", request);
RLog("EMS Starting async wget...");

emscripten_async_wget_data(request, NULL, _net_ems_onload, 
_net_ems_onerror);

RLog("EMS Waiting for async wget to complete...");

while(_wgetLock) { cu_sleep(100); }

RLog("EMS Async complete! Resuming main loop");

emscripten_resume_main_loop();

if(_success)
{
RLog("EMS Success! Response : \"%s\"", _lastResponse);
strcpy(response, _lastResponse);
return true;
}
else
{
RLog("EMS Fail :(");
return false;
}
}

Pardon the not-so-elegant construction of GET request, but that at least 
isn't the problem, I checked generated requests manually and they work in a 
browser or in terminal using the real wget. Am I doing something wrong 
here? And btw, cu_sleep is just my multi-platform wrap of sleep functions, 
it resolves to nanosleep(). Is there a better way of waiting for async 
request to complete? I need this net_httpGet function to effectively be 
blocking, to wait and return when the request is complete. I tried no 
waiting, this function returns false immediately, but still, only error 
callback gets called afterwards, always, and I don't see any way of finding 
out exactly what kind of error occured. Also, RLog is just my logging 
system, same signature as printf.

On Thursday, November 13, 2014 11:24:15 AM UTC+1, Sergey Kurdakov wrote:
>
> Hi Alexandar.
>
> won't emscripten_async_wget serve your needs? it sends requests to server 
> and params can adjast which type of request is sent.
>
> Regards
> Sergey
>
> On Thu, Nov 13, 2014 at 11:58 AM, Aleksandar Stančić <[email protected] 
> <javascript:>> wrote:
>
>> Hi! I'm writing a game in C++/OGL (to be published on the web through 
>> Emscripten) that needs some interaction with a server, written in 
>> PHP/MySQL. I already prepared the database and PHP code, and now, the 
>> surprisingly difficult part is connecting the two. Is there a simple way of 
>> loading a web page with a few POST strings in C++/Emscripten? Two-to-three 
>> strings go in (POST) and I need to read one string or integer on the output 
>> (HTTP response). emscripten_wget just hangs on me, and mixing JS + C++ 
>> seems to be able to transfer only numbers (ASM). Can someone point me in 
>> the right direction? I feel like I'm missing something obvious, and I 
>> wouldn't really know, since I'm not much of a web developer. Thanks.
>>
>> Just to clarify, I have a PHP page like this:
>>
>> $a = $_POST['a']  // a and b need to come from C++
>> $b = $_POST['b']
>>
>>
>>
>> // do stuff with $a & $b
>>
>>
>> echo($c)  // a string, under 100 regular ASCII chars, nothing special
>>
>> So, I need to do a HTTP POST request (I can switch to GET if that'll make 
>> things easier), and read a single line of text back into C++.  What would 
>> be a good way of doing that? I hate to be *that guy**,* but it is kind 
>> urgent (I really wrote myself into a corner with this one, I expected this 
>> to be easy), so any tips are greatly appreciated.
>>
>>  -- 
>> You received this message because you are subscribed to the Google Groups 
>> "emscripten-discuss" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to