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支持這些庫。 如果有人已經試圖做這樣的事情? 在此先感謝!
你看過Unity Networking嗎?如果你的客戶可以支持它,我聽說人們在HoloLens上有很好的結果。 https://unity3d.com/learn/tutorials/topics/multiplayer-networking –
謝謝。我會檢查這一點。這個話題很奇怪。沒有文檔,沒有例子,沒有主題......奇怪的... – Silvering