2017-02-02 82 views
1

我正在嘗試爲我的Hololens創建客戶端應用程序。這裏是我的代碼客戶端應用程序Hololens

using UnityEngine; 
using System.Collections; 
using System; 
using System.IO; 
using System.Net.Sockets; 



public class networkSocket : MonoBehaviour 
{ 
    public String host = "127.0.0.1"; 
    public Int32 port = 8030; 

    internal Boolean socket_ready = false; 
    internal String input_buffer = ""; 
    TcpClient tcp_socket; 
    NetworkStream net_stream; 

    StreamWriter socket_writer; 
    StreamReader socket_reader; 



    void Update() 
    { 
     string received_data = readSocket(); 
     string key_stroke = Input.inputString; 

     // Collects keystrokes into a buffer 
     if (key_stroke != ""){ 
      input_buffer += key_stroke; 

      if (key_stroke == "\n"){ 
       // Send the buffer, clean it 
       Debug.Log("Sending: " + input_buffer); 
       writeSocket(input_buffer); 
       input_buffer = ""; 
      } 

     } 


     if (received_data != "") 
     { 
      // Do something with the received data, 
      // print it in the log for now 
      Debug.Log(received_data); 
     } 
    } 


    void Awake() 
    { 
     setupSocket(); 
    } 

    void OnApplicationQuit() 
    { 
     closeSocket(); 
    } 

    public void setupSocket() 
    { 
     try 
     { 
      tcp_socket = new TcpClient(host, port); 

      net_stream = tcp_socket.GetStream(); 
      socket_writer = new StreamWriter(net_stream); 
      socket_reader = new StreamReader(net_stream); 

      socket_ready = true; 
     } 
     catch (Exception e) 
     { 
      // Something went wrong 
      Debug.Log("Socket error: " + e); 
     } 
    } 

    public void writeSocket(string line) 
    { 
     if (!socket_ready) 
      return; 

     line = line + "\r\n"; 
     socket_writer.Write(line); 
     socket_writer.Flush(); 
    } 

    public String readSocket() 
    { 
     if (!socket_ready) 
      return ""; 

     if (net_stream.DataAvailable) 
      return socket_reader.ReadLine(); 

     return ""; 
    } 

    public void closeSocket() 
    { 
     if (!socket_ready) 
      return; 

     socket_writer.Close(); 
     socket_reader.Close(); 
     tcp_socket.Close(); 
     socket_ready = false; 
    } 

} 

不過,我收到這樣的錯誤:類型或命名空間名稱「的TcpClient」無法找到與同爲「的NetworkStream」

我想,主要的問題是Hololens沒有按由於UWP支持這些庫。 如果有人已經試圖做這樣的事情? 在此先感謝!

+0

你看過Unity Networking嗎?如果你的客戶可以支持它,我聽說人們在HoloLens上有很好的結果。 https://unity3d.com/learn/tutorials/topics/multiplayer-networking –

+0

謝謝。我會檢查這一點。這個話題很奇怪。沒有文檔,沒有例子,沒有主題......奇怪的... – Silvering

回答

0

我們在項目中遇到同樣的問題。我們通過評論代碼解決了這個問題,然後在Unity中構建。在將它部署到HoloLens之前,我們對代碼進行了分解。問題是Unity不使用.net。