无法从外部服务器读取数据到Hololens

时间:2017-01-11 12:19:35

标签: c# uwp tcpclient hololens

我写了一些代码来将数据从外部服务器传输到Hololens。我能够将Hololens连接到服务器。但是我在将数据从服务器发送到Hololens时遇到了问题。每当我调用func handleLongGesture(gesture: UILongPressGestureRecognizer) { switch(gesture.state) { case UIGestureRecognizerState.Began: guard let selectedIndexPath = self.collectionView.indexPathForItemAtPoint(gesture.locationInView(self.collectionView)) else { break } collectionView.beginInteractiveMovementForItemAtIndexPath(selectedIndexPath) case UIGestureRecognizerState.Changed: collectionView.updateInteractiveMovementTargetPosition(gesture.locationInView(gesture.view!)) case UIGestureRecognizerState.Ended: collectionView.endInteractiveMovement() default: collectionView.cancelInteractiveMovement() } } 功能时,它甚至没有连接(它打印未连接)。

我对ReadData和团结并不熟悉,也无法解决这个问题。 我正在使用c#和DataReader类分别连接和读取数据。函数StreamSocketConnect()方法与服务器连接,然后我在start()方法中调用ReadData函数,以便在每一帧从服务器获取数据。我附上了我的代码文件。 你能帮助我解决我的问题吗,提前谢谢你。

update()

编辑:1。我怀疑using System.Collections; using System.Collections.Generic; using System.Net.Sockets; using System.Threading; using System.Text; using System.Net; using System; using UnityEngine; #if !UNITY_EDITOR && UNITY_METRO using Windows.Networking.Sockets; using Windows.Storage.Streams; using Windows.Networking; using Windows.Foundation; #endif public class TCPclientRead : MonoBehaviour { public string ServerIP = "10.1.2.35"; [Tooltip("The connection port on the machine to use.")] public int ConnectionPort = 11000; private string data = "connected" ; public TextMesh mesh; private bool connected = false; #if !UNITY_EDITOR && UNITY_METRO private StreamSocket networkConnection; /// <summary> /// Temporary buffer for the data we are recieving. /// </summary> //private byte[] dataBuffer; public void Connect( ) { // Setup a connection to the server. HostName networkHost = new HostName(ServerIP.Trim()); //HostName networkHost = new HostName( IPAddress.Any.ToString()); networkConnection = new StreamSocket(); // Connections are asynchronous. // !!! NOTE These do not arrive on the main Unity Thread. Most Unity operations will throw in the callback !!! IAsyncAction outstandingAction = networkConnection.ConnectAsync(networkHost, ConnectionPort.ToString()); AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(NetworkConnectedHandler); outstandingAction.Completed = aach; } public void NetworkConnectedHandler(IAsyncAction asyncInfo, AsyncStatus status) { if (status == AsyncStatus.Completed) { connected = true; // Here Just display connected } else { connected = false; Debug.Log("Failed to establish connection. Error Code: " + asyncInfo.ErrorCode); // In the failure case we'll requeue the data and wait before trying again. networkConnection.Dispose(); } } public void ReadData() { if(connected) { DataReader reader = new DataReader(networkConnection.InputStream); reader.InputStreamOptions = InputStreamOptions.Partial; reader.LoadAsync(512); data = reader.ReadString(512); } } private void Start() { mesh = gameObject.GetComponent<TextMesh>(); Connect(); } private void Update() { if (connected) { mesh.text = data; } else mesh.text = "Not Connected"; ReadData(); } #endif } 是否需要异步调用因此我更新了代码,但即使现在也无法正常工作。 2.我正在使用Unity Editor,我已启用所需的设置,我可以连接到服务器。只是我无法传输数据。 3.我使用netcat创建服务器。

我的更新代码

ReadData()

1 个答案:

答案 0 :(得分:1)

我怀疑你没有启用UWP功能&#34; InternetClient&#34;这将阻止它实际连接到远程服务器。您没有提到您正在使用的工具链,但如果您在Unity中,请检查Build Settings - &gt;发布设置 - &gt; Windows应用商店 - &gt;发布设置 - &gt;能力。如果您在Visual Studio中工作,则可以在项目属性中进行调整。