DataReader Hololens

Posted

技术标签:

【中文标题】DataReader Hololens【英文标题】: 【发布时间】:2018-01-22 08:03:53 【问题描述】:

我正在尝试在 Hololens 上创建一个 udp 客户端/服务器。我在这个项目背后的想法是在 ios 应用程序和 Hololens 之间进行通信。这是我的代码:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

#if !UNITY_EDITOR
    using Windows.Networking;
    using Windows.Networking.Sockets;
    using Windows.Storage.Streams;
#endif

public class server : MonoBehaviour

    public Text monTexte;

#if !UNITY_EDITOR
        StreamSocket socket;
        StreamSocketListener listener;
        String port;
        String message;
#endif

    // Use this for initialization
    void Start()
    
#if !UNITY_EDITOR
        listener = new StreamSocketListener();
        port = "12345";
        listener.ConnectionReceived += Listener_ConnectionReceived;
        listener.Control.KeepAlive = false;

        Listener_Start();
#endif
    

#if !UNITY_EDITOR
    private async void Listener_Start()
    
        Debug.Log("Listener started");
        try
        
            await listener.BindServiceNameAsync(port);
        
        catch (Exception e)
        
            Debug.Log("Error: " + e.Message);
        

        Debug.Log("Listening");
    

    private async void Listener_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
    
        Debug.Log("Connection received");

        try
        
            while (true) 

                    using (var dw = new DataWriter(args.Socket.OutputStream))
                    
                        dw.WriteString("salut");
                        await dw.StoreAsync();
                        dw.DetachStream();
                      

                    using (var dr = new DataReader(args.Socket.InputStream))
                    
                        var receivedStrings = "";

                        while (dr.UnconsumedBufferLength > 0)
                        
                            uint bytesToRead = dr.ReadUInt32();
                            receivedStrings += dr.ReadString(bytesToRead) + "\n";
                            Debug.Log(receivedStrings);
                            monTexte.text = receivedStrings;
                        
                    
            
        
        catch (Exception e)
        
            Debug.Log("iPhone disconnected!!!!!!!! " + e);
        

    

#endif

我设法发送消息,现在我不知道为什么我从来没有收到来自客户端的消息... 我创建了一个 DataReader,但它似乎从不听。任何的想法 ?谢谢!

【问题讨论】:

【参考方案1】:

我找到了解决方法,这是我的完整代码:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

#if !UNITY_EDITOR
    using Windows.Networking;
    using Windows.Networking.Sockets;
    using Windows.Storage.Streams;
#endif

public class server : MonoBehaviour

    public Text monTexte;
    public String _input = "Waiting";

#if !UNITY_EDITOR
        StreamSocket socket;
        StreamSocketListener listener;
        String port;
        String message;
#endif

    // Use this for initialization
    void Start()
    
#if !UNITY_EDITOR
        listener = new StreamSocketListener();
        port = "12345";
        listener.ConnectionReceived += Listener_ConnectionReceived;
        listener.Control.KeepAlive = false;

        Listener_Start();
#endif
    

#if !UNITY_EDITOR
    private async void Listener_Start()
    
        Debug.Log("Listener started");
        try
        
            await listener.BindServiceNameAsync(port);
        
        catch (Exception e)
        
            Debug.Log("Error: " + e.Message);
        

        Debug.Log("Listening");
    

    private async void Listener_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
    
        Debug.Log("Connection received");

        try
        
            while (true) 

                    using (var dw = new DataWriter(args.Socket.OutputStream))
                    
                        dw.WriteString("salut");
                        await dw.StoreAsync();
                        dw.DetachStream();
                      

                    using (var dr = new DataReader(args.Socket.InputStream))
                    
                        dr.InputStreamOptions = InputStreamOptions.Partial;
                        await dr.LoadAsync(12);
                        var input = dr.ReadString(12);
                        Debug.Log("received: " + input);
                        _input = input;

                    
            
        
        catch (Exception e)
        
            Debug.Log("iPhone disconnected!!!!!!!! " + e);
        

    

#endif

     void Update() 
        monTexte.text = _input;
    

【讨论】:

以上是关于DataReader Hololens的主要内容,如果未能解决你的问题,请参考以下文章

dataset 和 datareader对象有啥区别

为啥 DataTable 比 DataReader 快

DataReader:指定的强制转换无效 (Int32)

ADO.NET 四(DataReader)

寻找重置或清除 DataReader 的方法

没有名为 pandas_datareader 的模块