Hi,
I was trying to write a login system in my game connected to my own server. After searching online, I used the MySQL database and python flask on my server to receive and give the information.
The question is: The server got the request which had the information I entered in the game and should return a JSON message, but the game did not receive it. The response function I bound to the request is never executed in c++.
Here is the part of the code I wrote.
In HTTPService.cpp:
FHttpRequestRef UHTTPService::RequestWithRoute(FString Subroute)
{
FHttpRequestRef Request = FHttpModule::Get().CreateRequest();
FString TargetURL = TEXT("My IP Address") + Subroute;
Request->SetURL(TargetURL);
UE_LOG(LogTemp, Warning, TEXT("Request URL is: %s"), *Request->GetURL());
SetRequestHeader(Request);
return Request;
}
void UHTTPService::SetRequestHeader(FHttpRequestRef& Request)
{
Request->SetHeader(TEXT("User-Agent"), TEXT("X-UnrealEngine-Agent"));
Request->SetHeader(TEXT("Content-Type"), TEXT("application/json"));
Request->SetHeader(TEXT("Accepts"), TEXT("application/json"));
}
FHttpRequestRef UHTTPService::GetRequest(FString Subroute)
{
FHttpRequestRef Request = RequestWithRoute(Subroute);
Request->SetVerb("GET");
return Request;
}
FHttpRequestRef UHTTPService::PostRequest(FString Subroute, FString JsonString)
{
FHttpRequestRef Request = RequestWithRoute(Subroute);
Request->SetVerb("POST");
Request->SetContentAsString(JsonString);
return Request;
}
void UHTTPService::Send(FHttpRequestRef& Request)
{
Request->ProcessRequest();
}
//This part is executed.
void UHTTPService::StartLogin(FLogin_Request LoginCredentials)
{
FString JsonString;
GetJsonStringFromStruct<FLogin_Request>(LoginCredentials, JsonString);
UE_LOG(LogTemp, Warning, TEXT("JsonString is: %s"), *JsonString);
TSharedRef<IHttpRequest> Request = PostRequest("login", JsonString);
Request->OnProcessRequestComplete().BindUObject(this, &UHTTPService::LoginResponse);
Send(Request);
}
// This method is never executed
void UHTTPService::LoginResponse(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
if (!ResponseIsValid(Response, bWasSuccessful)) return;
FLogin_Response LoginResponse;
GetStructFromJsonString<FLogin_Response>(Response, LoginResponse);
UE_LOG(LogTemp, Warning, TEXT("Id is: %d"), LoginResponse.Id);
UE_LOG(LogTemp, Warning, TEXT("Name is: %s"), *LoginResponse.UserName);
}
bool UHTTPService::ResponseIsValid(FHttpResponsePtr Response, bool bWasSuccessful)
{
if (!bWasSuccessful || !Response.IsValid()) return false;
if (EHttpResponseCodes::IsOk(Response->GetResponseCode())) return true;
else {
UE_LOG(LogTemp, Warning, TEXT("Http response return error code: %d"), Response->GetResponseCode());
return false;
}
}
On the server, I wrote these:
login.py:
@app.route('/login', methods=['GET', 'POST'])
def login():
content = request.get_json(force=True)
if request.method == 'POST':
print ('Incoming request...')
username = content['username']
password = content['password']
print ('The username:', username, ' and password: ', password)
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM accounts WHERE username = %s AND password = %s', (username, password))
account = cursor.fetchone()
if account:
print ('Found user in database...')
session['loggedin'] = True
session['username'] = account['username']
session['id'] = account['id']
print ('Returning the message...')
return jsonify({'id':account['id'], 'username':account['username'], 'message':'Logged in successfully.'})
else:
print ('Did not find the user in database...')
print ('Sending back error message...')
message = {'message':'Incorrect username or password.'}
print ('Sending back error message...')
return jsonify(message)
The result in the server when running “login.py” is this:
The output log in the game is:
It stops here, and not executed the “LoginResponse” method.
Hope this information can help you understand what I did and please give me some advice on how to fix the problem. Thanks.