UE4与WEB服务器交互(json)
2016-04-16 14:10
681 查看
概述
制作游戏在很多情况下需要和WEB服务器进行交互,最常见的是在做Demo时需要通过游戏向WEB服务器传递数据(登录/注册验请求),WEB服务器处理(操作数据库)之后返回结果并调用指定的方法。
该教程简单介绍了如何通过UE4向WEB服务器(PHP)发送json数据包及回调方法。
添加模块和头文件引用
在代码编辑器中打开项目解决方案,在<Solution
Name>/Source/<ProjectName>路径下,找到并打开<ProjectName>.Build.cs文件,添加HTTP模块:
[C++]纯文本查看复制代码
然后在需要实现该功能的类文件中添加如下的头文件引用:
[C++]纯文本查看复制代码
创建json数据包
数据内容为:
[C++]纯文本查看复制代码
创建:
[C++]纯文本查看复制代码
至此,json数据包准备完成。
准备接收json数据包的PHP网页
本示例中使用了PHP,你可以选择使用搭建动态网站或者服务器的开源软件,比如说wamp/lamp等在本机上建立一个WEB服务器来解析PHP页面。
创建mywebpage.php文件,并添加如下代码:
[PHP]纯文本查看复制代码
POST数据
将通过如下的代码将上面准备好的json数据包提交给http://localhost/mywebpage.php
SetHeader:可以设置POST数据的格式
SetURL:可以指定用于处理上传数据的链接
SetVerb:可以设置POST/PUT/GET
SetContentAsString:用于填充上传的数据内容
OnProcessRequestComplete().BindUObject用于指定在发送请求之后的回调方法。
[C++]纯文本查看复制代码
关于回调函数的结构:void
HttpCompleteCallback(FHttpRequestPtrRequest,FHttpResponsePtrResponse,boolbWasSuccessful);示例:
[C++]纯文本查看复制代码
一旦发送出请求后肯定会调用HttpCompleteCallback方法,WEB服务器处理的数据结果包含在Response参数中,可以通过Response->GetContentAsString()来获取返回的字符串,比如在本例中是StormUnited。
总结
联系我:bm0nkey
常见问题及参考
https://answers.unrealengine.com/questions/4383/sending-an-fstring-via-sockets.html
https://answers.unrealengine.com/questions/4406/need-help-with-json-objects.html
https://answers.unrealengine.com/questions/2830/best-way-to-perform-a-http-request.html
https://answers.unrealengine.com/questions/3530/does-httprequest-use-httpmanager-by-default.html
https://answers.unrealengine.com/questions/2830/best-way-to-perform-a-http-request.html
https://answers.unrealengine.com/questions/23170/making-http-calls.html
https://answers.unrealengine.com/questions/31079/http%E9%80%9A%E4%BF%A1%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6.html
https://answers.unrealengine.com/questions/26704/trouble-deserializing-json.html
https://answers.unrealengine.com/questions/2830/best-way-to-perform-a-http-request.html
制作游戏在很多情况下需要和WEB服务器进行交互,最常见的是在做Demo时需要通过游戏向WEB服务器传递数据(登录/注册验请求),WEB服务器处理(操作数据库)之后返回结果并调用指定的方法。
该教程简单介绍了如何通过UE4向WEB服务器(PHP)发送json数据包及回调方法。
添加模块和头文件引用
在代码编辑器中打开项目解决方案,在<Solution
Name>/Source/<ProjectName>路径下,找到并打开<ProjectName>.Build.cs文件,添加HTTP模块:
[C++]纯文本查看复制代码
1 | PrivateDependencyModuleNames.AddRange( new string[] { "HTTP" }); |
2 | PrivateIncludePathModuleNames.AddRange( new string[] { "HTTP" }); |
[C++]纯文本查看复制代码
1 | #include "Http.h" |
2 | #include "Json.h" |
数据内容为:
[C++]纯文本查看复制代码
1 | { "user" : "StormUnited" } |
[C++]纯文本查看复制代码
1 | // CreateawriterandholditinthisFString |
2 | FString JsonStr; |
3 | TSharedRef< TJsonWriter< TCHAR , TCondensedJsonPrintPolicy< TCHAR > >>JsonWriter=TJsonWriterFactory< TCHAR , TCondensedJsonPrintPolicy< TCHAR > >::Create(&JsonStr); |
4 | JsonWriter->WriteObjectStart(); |
5 | JsonWriter->WriteValue(TEXT( "user" ), TEXT( "StormUnited" )); |
6 | JsonWriter->WriteObjectEnd(); |
7 |
8 | // |
9 | JsonWriter->Close(); |
准备接收json数据包的PHP网页
本示例中使用了PHP,你可以选择使用搭建动态网站或者服务器的开源软件,比如说wamp/lamp等在本机上建立一个WEB服务器来解析PHP页面。
创建mywebpage.php文件,并添加如下代码:
[PHP]纯文本查看复制代码
1 | <?php |
2 | // 首先接收上传的数据 |
3 | $post_data = file_get_contents ( ' ); |
4 | // 解析json字符串 |
5 | $obj = json_decode( $post_data ); |
6 | // 获取包含在Json字符串中的数据 |
7 | echo $obj ->{ 'user' }; |
8 | ?> |
将通过如下的代码将上面准备好的json数据包提交给
SetHeader:可以设置POST数据的格式
SetURL:可以指定用于处理上传数据的链接
SetVerb:可以设置POST/PUT/GET
SetContentAsString:用于填充上传的数据内容
OnProcessRequestComplete().BindUObject用于指定在发送请求之后的回调方法。
[C++]纯文本查看复制代码
1 | TSharedRef<IHttpRequest> |
2 | HttpRequest->SetHeader(TEXT( "Content-Type" ), TEXT( "application/json; charset=utf-8" )); |
3 | HttpRequest->SetURL(TEXT( " )); |
4 | HttpRequest->SetVerb(TEXT( "POST" )); |
5 | HttpRequest->SetContentAsString(JsonStr); |
6 | HttpRequest->OnProcessRequestComplete().BindUObject( this , &ASUMiniGameMode::HttpCompleteCallback); |
7 | HttpRequest->ProcessRequest(); |
HttpCompleteCallback(FHttpRequestPtrRequest,FHttpResponsePtrResponse,boolbWasSuccessful);示例:
[C++]纯文本查看复制代码
01 | void ASUMiniGameMode::HttpCompleteCallback(FHttpRequestPtr bool bWasSuccessful) |
02 | { |
03 | FString "" ; |
04 |
05 | // |
06 | if (!Response.IsValid()) |
07 | { |
08 | MessageBody "{\"success\":\"Error: ; |
09 | } |
10 | else if (EHttpResponseCodes::IsOk(Response->GetResponseCode())) |
11 | { |
12 | MessageBody |
13 | } |
14 | else |
15 | { |
16 | MessageBody "{\"success\":\"HTTP ), Response->GetResponseCode()); |
17 | } |
18 | } |
总结
联系我:
常见问题及参考
相关文章推荐
- 【Arduino官方教程第一辑】示例程序 1-1 读取模拟信号、串口操作
- iOS 动画总结----UIView动画
- Android UI基础之Spinner
- UI- 五种手势识别总结
- poj 2264 Advanced Fruits lcs
- Android UI高级之Menu
- [学习笔记]easyui 为表格行添加事件
- Android UI高级之Notification
- UI- UINavigationController UITabBarController 使用总结
- How do I setup Git for an UE4 project?
- (LeetCode 304) Range Sum Query 2D - Immutable
- HDU-3732 Ahui Writes Word
- Android进阶-UIL分析
- JAVA——String与StringBuffer以及StringBuilder
- UVA 1151 Buy or Build(生成树+二进制枚举)
- OpenStack 安装教程(使用Fuel )
- Light OJ 1420 - Subsequences forming Strings (DP)
- Code Forces 644B Processing Queries
- Code Forces 644B Processing Queries
- Jersey(1.19.1) - Extracting Request Parameters