Simple communication process, unity<--->node
First build the node server and install express and other dependencies.
Node code:
var express=require('express');
var fs=require('fs');
var url=require('url');
var bodyParser=require('body-parser');
var app=express();
app.use(bodyParser.urlencoded({extended:false})) //This sentence is very important
//Receive the user id, and go to the database query
app.post('/SkierCamInfo',function(req,res){
console.log(req.body);
var data=JSON.stringify(req.body);
console.log("Received data: "+data);
fs.readFile(__dirname+"/"+"SkierCamInfo2.json","utf8",function(err,data){ //Query the corresponding video url address from the database according to the user id
data=JSON.parse(data);
res.send(JSON.stringify(data));
res.end();
})
})
var server=app.listen(8081,function(){
var host=server.address().address
var port=server.address().port
console.log("Server Is Running Now!");
})
Unity side code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HttpTest : MonoBehaviour {
void Start () {
StartCoroutine(SendID());
}
//You can also use UnityWebRequest
IEnumerator SendID() {
string url = "http://127.0.0.1:8081/SkierCamInfo";
WWWForm form = new WWWForm();
form.AddField("ID","181xxxxxxx");
form.AddField("name", "LiMing");
WWW www = new WWW(url,form);
yield return www;
if (www.error != null)
{
Debug.Log(www.error);
}
else {
Debug.Log(www.text);
}
}
}
Local test Json file, readFile:
{
"cameraId":"p_cam_20200114",
"cameraType":2,
"rtmpUrl":"https://xxxxxx.mp4",
}