Golang服务器,Unity客户端通信

发表于2017-10-11
评论0 2.2k浏览

编辑器有:

1 liteIDE

2、Sublime Text 2

3、Google Go language IDE built using the IntelliJ Platform

 

这个是需要手动配置的!


下面就介绍三种Unity客户端通信的例子:

一、Unity 客户端与 Golang 伺服器做通信

服务器执行结果

Unity客户端执行结果

 

程序代码:

//----------------------------------- Unity C# 客戶端

  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Net.Sockets;  
  4. using System.IO;  
  5.   
  6. public class Server1 : MonoBehaviour  
  7. {  
  8.     readonly string _ip = "127.0.0.1"// 改为自己对外的 IP  
  9.     readonly int _port = 1024;  
  10.   
  11.     void OnGUI()  
  12.     {  
  13.         if (GUI.Button(new Rect(10, 10, 100, 100), "Send Public IP"))  
  14.         {  
  15.             Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  
  16.             socket.Connect(_ip, _port);  
  17.             NetworkStream stream = new NetworkStream(socket);  
  18.             StreamWriter sw = new StreamWriter(stream);  
  19.             StreamReader sr = new StreamReader(stream);  
  20.   
  21.             sw.WriteLine("你好服务器,我是客户端。");  
  22.             sw.Flush();  
  23.   
  24.             string st = sr.ReadLine();  
  25.             print(st);  
  26.   
  27.             sw.Close();  
  28.             stream.Close();  
  29.             socket.Close();  
  30.         }  
  31.     }  
  32. }  


//-------------------------------------- Golang , Go語言 伺服器

  1. package main  
  2.   
  3. import (  
  4.     "bufio"  
  5.     "net"  
  6. )  
  7.   
  8. func main() {  
  9.   
  10.     listener, _ := net.Listen("tcp", ":1024")  
  11.     println("启动服务器...")  
  12.   
  13.     for {  
  14.         conn, _ := listener.Accept() // 持续监听客户端连接  
  15.         go ClientLogic(conn)  
  16.     }  
  17. }  
  18.   
  19. func ClientLogic(conn net.Conn) {  
  20.   
  21.     // 从客户端接受数据  
  22.     s, _ := bufio.NewReader(conn).ReadString('\n')  
  23.     println("由客户端发来的消息:", s)  
  24.   
  25.     // 发送消息给客户端  
  26.     conn.Write([]byte("东东你好\n"))  
  27.   
  28.     // 关闭连接  
  29.     conn.Close()  
  30. }  


 

二、上传图片给服务器(后端)Server 使用 Golang

运行结果  就是把内容 添加到了这里!

  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class Server2 : MonoBehaviour  
  5. {  
  6.     // 编辑器赋值,图片要设置为 Advanced 并且选中 可读可写 (否则会失败)  
  7.     public Texture2D t;   
  8.   
  9.     IEnumerator Start()  
  10.     {  
  11.         WWWForm wwwF = new WWWForm();  
  12.         wwwF.AddBinaryData("file", t.EncodeToPNG(), "A.png");  
  13.         WWW www = new WWW("http://127.0.0.1:8080/upload", wwwF);  
  14.         yield return www;  
  15.         print("Upload Finish !!");  
  16.     }  
  17. }  


 


  1. // 以下為 Golang Web 伺服器  
  2. package main  
  3. import (  
  4.     "io/ioutil"  
  5.     "net/http"  
  6.     "os"  
  7. )  
  8. func uploadHandle(w http.ResponseWriter, r *http.Request) {  
  9.   
  10.     file, head, _ := r.FormFile("file")  
  11.     defer file.Close()  
  12.     bytes, _ := ioutil.ReadAll(file)  
  13.     ioutil.WriteFile(head.Filename, bytes, os.ModeAppend)  
  14. }  
  15. func main() {  
  16.     http.HandleFunc("/upload", uploadHandle)  
  17.     http.ListenAndServe(":8080", nil)  
  18. }  


 

三、传参数给 Golang Web 服务器,并回传讯息给 Unity

执行结果:

  1. Golang Server  code  
  2.   
  3. package main  
  4. import (  
  5.     "fmt"  
  6.     "net/http"  
  7. )  
  8.   
  9. func loadin(w http.ResponseWriter, r *http.Request) {  
  10.     username := r.FormValue("username")  
  11.     password := r.FormValue("password")  
  12.     fmt.Fprintf(w, "<html><body>")  
  13.     fmt.Fprintf(w, "Hello, %s ! <br/>", username)  
  14.     fmt.Fprintf(w, "Your password is : %s!", password)  
  15.     fmt.Fprintf(w, "</body></html>")  
  16. }  
  17.   
  18. func main() {  
  19.     http.HandleFunc("/loadin", loadin)  
  20.     http.ListenAndServe(":8080", nil)  
  21. }  


 


  1. Unity C# code  
  2.   
  3. using UnityEngine;  
  4. using System.Collections;  
  5.   
  6. public class Server3 : MonoBehaviour  
  7. {  
  8.   
  9.     IEnumerator Start()  
  10.     {  
  11.         WWWForm wwwF = new WWWForm();  
  12.         wwwF.AddField("username""Loli");  
  13.         wwwF.AddField("password""Kitty");  
  14.         WWW www = new WWW("http://127.0.0.1:8080/loadin", wwwF);  
  15.         yield return www;  
  16.         print(www.text);  
  17.     }  
  18. }  
http://blog.csdn.net/u010019717/article/details/51626807

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引