Hololens开发(8) 使用WCF

继续之前的WCF。

现在知道脚本项目(Assembly-CSharp)无法添加服务引用,启动项目可以添加,那试试看在启动项目中添加服务引用,然后在脚本项目中通过接口的方式调用上层代码。

先确认原来的UWP程序能连接上。

可以。

因为又出现了那个VS崩溃问题,改为先用HololographicApp测试。

测试代码

public static class WCFTest
    {
        public static async void Test()
        {
            ServiceReference1.MyContractClient client = GetClient();
            await client.MyMethodAsync();
            await client.GetStringAsync(10, "A");
            await client.SendStringAsync("ABCD");
            await client.CloseAsync();
        }

        public static MyContractClient GetClient()
        {
            string ip = "192.168.253.1";
            NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
            binding.MaxReceivedMessageSize = int.MaxValue;
            EndpointAddress endpointAddress = new EndpointAddress("net.tcp://" + ip + ":8001/");
            MyContractClient currentClient = new MyContractClient(binding, endpointAddress);
            return currentClient;
        }
    }

直接加到AppView.Initialize中

        public void Initialize(CoreApplicationView applicationView)
        {
            applicationView.Activated += this.OnViewActivated;

            // Register event handlers for app lifecycle.
            CoreApplication.Suspending += this.OnSuspending;
            CoreApplication.Resuming += this.OnResuming;

            // At this point we have access to the device and we can create device-dependent
            // resources.
            deviceResources = new DeviceResources();

            main = new HolographicApp1Main(deviceResources);

            WCFTest.Test();
        }
部署运行,可以,服务端有收到消息!!!!!!!!

注意:必须到Package.appxmanifest中设置功能,要有Internet客户端服务器功能。

这下这条路看到希望了!!

然后在点击事件中添加测试代码(这个HololographicApp是默认的显示一个旋转矩形的例子,原本是响应点击事件移动矩形的)。

        public void PositionHologram(SpatialPointerPose pointerPose)
        {
            if (null != pointerPose)
            {
                // Get the gaze direction relative to the given coordinate system.
                Vector3 headPosition        = pointerPose.Head.Position;
                Vector3 headDirection       = pointerPose.Head.ForwardDirection;

                // The hologram is positioned two meters along the user's gaze direction.
                float   distanceFromUser    = 2.0f; // meters
                Vector3 gazeAtTwoMeters     = headPosition + (distanceFromUser * headDirection);

                // This will be used as the translation component of the hologram's
                // model transform.
                this.position = gazeAtTwoMeters;

                WCFTest.Test();
            }
        }

部署测试,没问题。

接下来到Unity生成的Hololens项目中测试,先还是加在App中Initialize后面,直接只是部署,避免又崩溃了。

运行,服务端没收到消息。

修改放置的地方,构造函数,SetWindow,Load,Run,CreateView都不行。

然后突然想到,Internet功能忘记设置了。

设置功能,还是加到Initialize中,部署,运行,可以了。有收到消息。

还是在各个地方添加测试函数,发送相应字符串到服务端。

结果:


可以做打印日志用了。

还差最后一步,要在脚本中调用WCF。

在脚本项目中添加文件IWCFClient

    public interface IWCFClient
    {
        void SendStringAsync(string msg);
    }

    public static class WCFClientFactory
    {
        public static IWCFClient Client;

        public static void SendMessage(string msg)
        {
            if (Client != null)
            {
                Client.SendStringAsync(msg);
            }
            else
            {
                Debug.Log(msg);
            }
        }
    }
在启动项目中添加WCFClient

    public class WCFClient : IWCFClient
    {
        public void SendStringAsync(string msg)
        {
            WCFTest.Test(msg);
        }
    }

在App的构造函数中添加初始化代码

WCFClientFactory.Client=new WCFClient();
在脚本中添加调用代码
public class WCFInfo : MonoBehaviour {

    public Text text;
	void Start () {
        Person person = new Person();
        person.Id = "Id1";
        person.Name = "Name";
        text.text += person.ToString();

        WCFClientFactory.SendMessage("WCFInfo.Start:" + person);
    }
部署运行,结果:


好,可以了。

接下来修改UI,做一下交付功能和传输测试。





 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值