一、搭建客戶端與服務(wù)器http通訊
1.在Nodejs中文官網(wǎng)Node.js 中文網(wǎng) (nodejs.com.cn),下載并安裝Nodejs
?2.在項(xiàng)目文件夾下新建WebServer文件夾,打開CMD窗口,在WebServer文件夾路徑下安裝express
?
?3.在WebServer文件夾中新建main.js文件,在main.js中編寫服務(wù)端腳本
var express = require("express");
var path=require("path")
var app = express();
app.listen(7777);//監(jiān)聽ip+端口 http://127.0.0.1:7777/
app.use("/", express.static(path.join(process.cwd(), "www_root")));//在WebServer文件夾中新建www_root文件夾
?4.在CMD中運(yùn)行main.js,打開瀏覽器,在地址欄中輸入http://127.0.0.1:7777/info.txt即可看到info.txt中的信息
?
?二、UnityWebRequest
- 構(gòu)建UnityWebRequest協(xié)議請求
- 發(fā)送請求:SendWebRequest異步
- 從客戶端傳數(shù)據(jù)到服務(wù)端UploadHandler或從服務(wù)端下載數(shù)據(jù)到客戶端DownloadHandler
使用下面的腳本方法可以獲取到百度的網(wǎng)頁信息
注:需要引入using?UnityEngine.Networking;命名空間
IEnumerator GetBaiduInfo()
{
UnityWebRequest req = UnityWebRequest.Get("http://www.baidu.com");
yield return req.SendWebRequest();
Debug.Log(req.downloadHandler.text);
}
三、發(fā)送數(shù)據(jù)到服務(wù)端
?1.修改main.js文件
var express = require("express");
var path=require("path")
var app = express();
app.listen(7777);//監(jiān)聽ip+端口 http://127.0.0.1:7777/
app.use("/", express.static(path.join(process.cwd(), "www_root")));//在WebServer文件夾中新建www_root文件夾
app.get("/", function (req, res) {
//console.log(req);//打印請求信息
console.log(req.query);
res.send("Received information!!");
});
修改腳本方法
IEnumerator GetUploadInfo()
{
UnityWebRequest req = UnityWebRequest.Get("http://127.0.0.1:7777/?name=123&pwd=321");
yield return req.SendWebRequest();
Debug.Log(req.downloadHandler.text);
}
2.執(zhí)行main.js,查看客戶端和服務(wù)端打印的信息
?
?四、從服務(wù)器上獲取文件信息
1.修改腳本方法
IEnumerator ReadResInfo()
{
UnityWebRequest req = UnityWebRequest.Get("http://127.0.0.1:7777/info.txt");
yield return req.SendWebRequest();
Debug.Log(req.downloadHandler.text);
}
2.執(zhí)行main.js,查看客戶端打印的信息
?五、從服務(wù)端下載資源
1.在服務(wù)端根目錄中新建Sounds文件夾,在該文件夾中放了一首音樂
?2.修改腳本方法
IEnumerator DownloadResfiles()
{
UnityWebRequest req = UnityWebRequest.Get("http://127.0.0.1:7777/Sounds/??? - asmr采耳助眠.mp3");
yield return req.SendWebRequest();
byte[] body = req.downloadHandler.data;
Debug.Log(Application.dataPath);//打印項(xiàng)目的Assets路徑,該路徑只在PC端有效
string fileName = Application.dataPath + "/Sounds/??? - asmr采耳助眠.mp3";
File.WriteAllBytes(fileName, body);//需要引入using System.IO;
}
3.執(zhí)行main.js,可以看到音樂被下載到了項(xiàng)目的Sounds文件夾中
?六、客戶端上傳文件到服務(wù)端
1.刪除服務(wù)端中Sounds文件夾中的音樂,從客戶端將音樂傳到服務(wù)端的Sounds文件夾中
修改main.js
var express = require("express");
var path=require("path")
var app = express();
app.listen(7777);//監(jiān)聽ip+端口 http://127.0.0.1:7777/
app.use("/", express.static(path.join(process.cwd(), "www_root")));//在WebServer文件夾中新建www_root文件夾,設(shè)置根目錄
app.get("/", function (req, res) {
//console.log(req);//打印請求信息
console.log(req.query);
res.send("Received information!!");
});
var fs = require("fs");
app.put("/UploadFile", function (req, res) {
//打開一個(gè)文件
var fd = fs.openSync("./www_root/Sounds/??? - asmr采耳助眠.mp3", "w");
req.on("data", function (data) {
fs.write(fd, data, 0, data.length, function () { });
});
req.on("end", function () {
res.send("UploadSucess");
fs.close(fd, function () { });
});
});
2.修改腳本方法
IEnumerator UploadResfiles()
{
string fileName = Application.dataPath + "/Sounds/??? - asmr采耳助眠.mp3";
byte[] body = File.ReadAllBytes(fileName);
UnityWebRequest req = UnityWebRequest.Put("http://127.0.0.1:7777/UploadFile", body);
yield return req.SendWebRequest();
Debug.Log(req.downloadHandler.text);
}
3.在CMD中執(zhí)行main.js,運(yùn)行客戶端,可以看到客戶端接收到服務(wù)端發(fā)來的信息,在服務(wù)端的Sounds文件中可以看到音樂被上傳了
?文章來源:http://www.zghlxwxcb.cn/news/detail-593878.html
參考:
【Unity】網(wǎng)絡(luò)進(jìn)階實(shí)戰(zhàn)(四): UnityWebRequest全功能實(shí)戰(zhàn)詳解_嗶哩嗶哩_bilibili文章來源地址http://www.zghlxwxcb.cn/news/detail-593878.html
到了這里,關(guān)于Unity UnityWebRequest使用http與web服務(wù)器通訊的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!