客户端应用程序 Hololens
Client side app 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'
也一样
我猜主要问题是由于 UWP,Hololens 不支持这些库。
如果有人已经尝试做这样的事情?
提前致谢!
我们在一个项目中遇到了同样的问题。我们通过注释掉代码解决了这个问题,然后在 Unity 中构建。在将其部署到 HoloLens 之前,我们对代码进行了反注释。问题是Unity不使用.net.
我正在尝试为我的 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'
也一样我猜主要问题是由于 UWP,Hololens 不支持这些库。 如果有人已经尝试做这样的事情? 提前致谢!
我们在一个项目中遇到了同样的问题。我们通过注释掉代码解决了这个问题,然后在 Unity 中构建。在将其部署到 HoloLens 之前,我们对代码进行了反注释。问题是Unity不使用.net.