安装node,配置环境
npm install express
服务器代码 webserver.js
var express = require("express")
var app = express()
var path = require("path")
//静态网页目录,process.cwd()为当前目录
app.use(express.static(path.join(process.cwd(),"www_root")))
//服务器端口
app.listen("6080");
// 设置我们的跨域访问
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1')
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
// end
//添加一个get请求接口,如果需要别的接口,修改login,或者在添加一个就可以了
app.get("/Login",function(request,response){
//打印客户端传来的数据,这里的数据是从url后面传进来的
console.log(request.query);
response.send("SUCCESS");
});
//添加一个post接口
app.post("/upload",function (request,response) {
//打印客户端传来的数据,这里的数据是从url后面传进来的
console.log(request.query);
request.on("data",function(data){
//客户端body里传过来的数据
console.log(data.toString());
//传给客户端的数据
var testData = {
"数据1":1,
"数据2":222,
};
//传一个数据给客户端
response.send(JSON.stringify(testData));
});
})
console.log("app启动")
客户端代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public enum ReqType {
www_get=1,
web_get=2,
www_post=3,
web_post=4,
}
public class WebRequst : MonoBehaviour {
int idx = 0;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space)) {
idx++;
if (idx > 4) {
idx = 1;
}
Req();
}
}
private void Req() {
switch ((ReqType)idx)
{
case ReqType.www_get: { StartCoroutine(GetSys()); } break;
case ReqType.web_get: { StartCoroutine(TestWeb()); } break;
case ReqType.www_post: { StartCoroutine(PostSys()); } break;
case ReqType.web_post: { StartCoroutine(TestWebPost()); } break;
}
}
IEnumerator GetSys()
{
//服务器地址
string url = "http://127.0.0.1:6080/";
//get请求的接口
string getInterface = "Login";
//传给服务器的数据,?开头,每条数据以key=value的形式用&拼接起来
string getParams = "?uname=dx&pwd=123456";
WWW _www = new WWW(url + getInterface + getParams);
yield return _www;
if (_www.isDone && string.IsNullOrEmpty(_www.error))
{
//服务器传过来的数据
Debug.Log(_www.text);
}
else
{
Debug.Log(_www.error);
}
}
IEnumerator TestWeb()
{
//服务器地址
string url = "http://127.0.0.1:6080/";
//get请求的接口
string getInterface = "Login";
//传给服务器的数据,?开头,每条数据以key=value的形式用&拼接起来
string getParams = "?uname=dx&pwd=123456";
UnityWebRequest _UnityWebRequest = UnityWebRequest.Get(url + getInterface + getParams);
yield return _UnityWebRequest.SendWebRequest();
if (_UnityWebRequest.isHttpError || _UnityWebRequest.isNetworkError)
{
Debug.Log(_UnityWebRequest.error);
}
else
{
Debug.Log(_UnityWebRequest.downloadHandler.text);
}
}
IEnumerator PostSys()
{
//服务器地址
string url = "http://127.0.0.1:6080/";
//get请求的接口
string getInterface = "upload";
//传给服务器的数据,?开头,每条数据以key=value的形式用&拼接起来
string getParams = "?key=post测试&文件名=log.txt";
WWWForm _WWWForm = new WWWForm();
//传给服务器的数据(就是body数据)
_WWWForm.AddField("log", "测试");
//unity封装的另一种添加方法
_WWWForm.AddBinaryData("log1", System.Text.Encoding.UTF8.GetBytes("测试2"));
//unity里默认添加了这个头了,就不需要添加了
//_WWWForm.headers.Add("Content-Type", "application/x-www-form-urlencoded");
//_WWWForm.headers.Add("Content-Length", _WWWForm.data.Length.ToString());
WWW _www = new WWW(url + getInterface + getParams, _WWWForm);
yield return _www;
if (_www.isDone && string.IsNullOrEmpty(_www.error))
{
Debug.Log(_www.text);
}
else
{
Debug.Log(_www.error);
}
}
IEnumerator TestWebPost()
{
//服务器地址
string url = "http://127.0.0.1:6080/";
//get请求的接口
string getInterface = "upload";
//传给服务器的数据,?开头,每条数据以key=value的形式用&拼接起来
string getParams = "?key=post测试&文件名=log.txt";
WWWForm _WWWForm = new WWWForm();
_WWWForm.AddField("log", "测试");
_WWWForm.AddBinaryData("log1", System.Text.Encoding.UTF8.GetBytes("测试2"));
UnityWebRequest _UnityWebRequest = UnityWebRequest.Post(url + getInterface + getParams, _WWWForm);
yield return _UnityWebRequest.SendWebRequest();
if (_UnityWebRequest.isHttpError || _UnityWebRequest.isNetworkError)
{
Debug.Log(_UnityWebRequest.error);
}
else
{
Debug.Log(_UnityWebRequest.downloadHandler.text);
}
}
}