clugg / sm-httpreq

Provides the ability to make simple HTTP requests via the socket extension in SourcePawn.
GNU General Public License v3.0
19 stars 5 forks source link

Freezing on socket connecting. #1

Closed redbaty closed 7 years ago

redbaty commented 7 years ago

Hello, I'm somewhat new to pawn and I'm trying to contact my api using the following code:

public void OnPluginStart()
{
  RegAdminCmd("sm_api_register", Command_Register, ADMFLAG_SLAY);
}

public Action Command_Register(int client, int args)
{
    PrintToServer("Requesting match ID from project nun");
    HTTPRequest req = HTTPRequest("GET", "http://projectnun.azurewebsites.net/api/heartbeat/register", "OnRequestComplete");
    req.debug = true;
    req.headers.SetString("User-Agent", "HTTPRequests for SourceMod");
    req.SendRequest();
}

public void OnRequestComplete(bool bSuccess, int iStatusCode, StringMap tHeaders, const char[] sBody, int iErrorType, int iErrorNum, any data)
{
        PrintToServer("Received");
}

however it'll never receive anything, the last thing on the output console is :

sm_api_register
Requesting match ID from project nun
httpreq::HTTPRequest 0 socket connecting to projectnun.azurewebsites.net:80

and it stays still. Am I missing something or is it really a bug ?

Anyway thanks for your attention and congratulations on your work so far

clugg commented 7 years ago

I never had subdomains in mind when developing this, which means it unfortunately isn't even close to following the HTTP spec for them. I originally created this include because I was experiencing issues with the SteamWorks extension. However, since then, those issues have been fixed or proven themselves minor enough for me to start using that extension again. For that reason, I'd recommend you consider using SteamWorks too - from what I know it officially supports the HTTP spec and has fairly similar syntax. Here's your code using SteamWorks:

#include <sourcemod>
#include <SteamWorks>

public void OnPluginStart()
{
    RegAdminCmd("sm_api_register", Command_Register, ADMFLAG_SLAY);
}

public Action Command_Register(int client, int args)
{
    PrintToServer("Requesting match ID from project nun");
    Handle req = SteamWorks_CreateHTTPRequest(k_EHTTPMethodGET, "http://projectnun.azurewebsites.net/api/heartbeat/register");
    SteamWorks_SetHTTPCallbacks(req, OnRequestComplete);
    SteamWorks_SetHTTPRequestHeaderValue(req, "User-Agent", "HTTPRequests for SourceMod");
    SteamWorks_SendHTTPRequest(req);
}

public int OnRequestComplete(Handle hRequest, bool bFailure, bool bRequestSuccessful, EHTTPStatusCode eStatusCode)
{
    int length;
    SteamWorks_GetHTTPResponseBodySize(hRequest, length);
    char[] sBody = new char[length];
    SteamWorks_GetHTTPResponseBodyData(hRequest, sBody, length);

    PrintToServer("Received");
}