Unity UnityWebRequest uses http to communicate with the web server

1. Build client-server http communication

1. Download and install Nodejs on Nodejs Chinese official website Node.js Chinese website (nodejs.com.cn)

 2. Create a new WebServer folder under the project folder, open the CMD window, and install express under the WebServer folder path

 

 3. Create a new main.js file in the WebServer folder, and write a server-side script in main.js

var express = require("express");
var path=require("path")
var app = express();
app.listen(7777);//监听ip+端口 http://127.0.0.1:7777/
app.use("/", express.static(path.join(process.cwd(), "www_root")));//在WebServer文件夹中新建www_root文件夹

 4. Run main.js in CMD, open the browser, and enter http://127.0.0.1:7777/info.txt in the address bar to see the information in info.txt

 

 Two, UnityWebRequest
  1. Construct UnityWebRequest protocol request
  2. Send request: SendWebRequest asynchronously
  3. Transfer data from the client to the server UploadHandler or download data from the server to the client DownloadHandler

Use the following script method to get Baidu's web page information

Note: need to introduce using  UnityEngine.Networking; namespace

IEnumerator GetBaiduInfo()
    {
        UnityWebRequest req = UnityWebRequest.Get("http://www.baidu.com");
        yield return req.SendWebRequest();
        Debug.Log(req.downloadHandler.text);
}

3. Send data to the server

 1. Modify the main.js file

var express = require("express");
var path=require("path")
var app = express();
app.listen(7777);//监听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. Execute main.js to view the information printed by the client and server

 

 4. Obtain file information from the server

1. Modify the script method

IEnumerator ReadResInfo()
    {
        UnityWebRequest req = UnityWebRequest.Get("http://127.0.0.1:7777/info.txt");
        yield return req.SendWebRequest();
        Debug.Log(req.downloadHandler.text);
}

2. Execute main.js to view the information printed by the client

 5. Download resources from the server

1. Create a new Sounds folder in the root directory of the server, and put a piece of music in this folder

 2. Modify the script method

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);//打印项目的Assets路径,该路径只在PC端有效
        string fileName = Application.dataPath + "/Sounds/안화화 - asmr采耳助眠.mp3";
        File.WriteAllBytes(fileName, body);//需要引入using System.IO;
}

3. Execute main.js, you can see that the music has been downloaded to the Sounds folder of the project

 Sixth, the client uploads files to the server

1. Delete the music in the Sounds folder on the server, and transfer the music from the client to the Sounds folder on the server

Modify main.js

var express = require("express");
var path=require("path")
var app = express();
app.listen(7777);//监听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!!");
});

var fs = require("fs");
app.put("/UploadFile", function (req, res) {
    //打开一个文件
    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. Modify the script method

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. Execute main.js in CMD, run the client, you can see that the client receives the information from the server, and you can see that the music has been uploaded in the Sounds file on the server

 

reference:

[Unity] Advanced network combat (4): UnityWebRequest full-featured combat detailed explanation_哔哩哔哩_bilibili

Guess you like

Origin blog.csdn.net/falsedewuxin/article/details/131719606