Exemple #1
0
        // Update is called once per frame
        void Update()
        {
            if (syncClient != null)
            {
                if (syncClient.Running)
                {
                    while (syncClient.SyncQueue.GetCount() > 0)
                    {
                        byte[] messageBytes = syncClient.SyncQueue.Dequeue();

                        // 处理消息
                        LiveMessage msg = LiveMessageManager.ParseMessage(messageBytes);
                        //Debug.Log("msg type=" + msg.type);
                        switch (msg.type)
                        {
                        case LiveMessageConstant.BEV_MESSAGE_TYPE_START:
                            synchronizing = true;
                            break;

                        case LiveMessageConstant.BEV_MESSAGE_TYPE_STOP:
                            synchronizing = false;
                            break;

                        case LiveMessageConstant.BEV_MESSAGE_TYPE_SET_ANCHOR:
                            LiveMessageSetAnchor msgSetAnchor = msg as LiveMessageSetAnchor;
                            // 这里也不能再同步了
                            synchronizing = false;
                            SetAnchors(msgSetAnchor);

                            // 重置同步消息序号
                            syncIndex = 0;

                            break;

                        case LiveMessageConstant.BEV_MESSAGE_TYPE_SAVE_ANCHOR:
                            LiveMessageSaveAnchor msgSaveAnchor = msg as LiveMessageSaveAnchor;
                            // 这里也不能再同步了
                            synchronizing = false;
                            SaveAnchors(msgSaveAnchor);
                            break;

                        case LiveMessageConstant.BEV_MESSAGE_TYPE_DOWNLOAD_ANCHOR:
                            DownloadAnchor();
                            break;

                        case LiveMessageConstant.BEV_MESSAGE_TYPE_REQUEST_SPATIAL_MAPPING:
                            SendSpatialMapping();
                            break;
                        }
                    }
                }
            }
        }
Exemple #2
0
        private void CheckMessage()
        {
            float realTime = Time.realtimeSinceStartup;

            if (server != null)
            {
                //Debug.Log("Server Listening? " + server.IsListening);
                if (hololensConnected && !hololensHasInit && !waiting)
                {
                    // 如果连上了,就开始初始化
                    SetHololensSynchronize(false);
                    SetAnchorToHololens(true);

                    // 这时需要把传输序列号清空,避免新接入的Hololens的同步消息被忽略
                    lastSyncIndex = int.MinValue;
                }

                while (SyncServer.SyncQueue.GetCount() > 0)
                {
                    byte[] messageBytes = SyncServer.SyncQueue.Dequeue();
                    //Debug.Log("receive [" + messageBytes.Length + "] type=" + messageBytes[0]);

                    // 处理消息
                    LiveMessage msg = LiveMessageManager.ParseMessage(messageBytes);

                    switch (msg.type)
                    {
                    case LiveMessageConstant.BEV_MESSAGE_TYPE_SYNCHRONIZE_ALL:
                        LiveMessageSynchronizeAll syncMsg = msg as LiveMessageSynchronizeAll;

                        DealSyncMessage(syncMsg);

                        break;

                    case LiveMessageConstant.BEV_MESSAGE_TYPE_DOWNLOAD_ANCHOR_FINISH:
                        LiveMessageDownloadFinish downloadFinishMsg = msg as LiveMessageDownloadFinish;
                        waiting = false;
                        if (downloadFinishMsg.result.success)
                        {
                            waitingString = "Download Anchors Success! ";
                        }
                        else
                        {
                            waitingString = downloadFinishMsg.result.errorString;
                        }
                        break;

                    case LiveMessageConstant.BEV_MESSAGE_TYPE_SET_ANCHOR_FINISH:
                        LiveMessageSetAnchorFinish anchorFinishMsg = msg as LiveMessageSetAnchorFinish;
                        spectatorViewVersion = anchorFinishMsg.version;
                        if (spectatorViewVersion < LiveHololens.version)
                        {
                            // SpectatorView版本太低!
                            liveUI.ShowInfoDialog("Spectator View's verion is too old!");
                            hololensConnected = false;
                            StopHololesServer();
                        }

                        waiting         = false;
                        waitingString   = "init OK!";
                        hololensHasInit = true;

                        // 初始化完成,就先开启位置传输
                        SetHololensSynchronize(true);
                        break;

                    case LiveMessageConstant.BEV_MESSAGE_TYPE_SAVE_ANCHOR_FINISH:
                        LiveMessageSaveAnchorFinish savehMsg = msg as LiveMessageSaveAnchorFinish;

                        waiting = false;
                        if (savehMsg.result.success)
                        {
                            waitingString = "Save Anchor OK!";

                            // 初始化完成,就先开启位置传输
                            // 此功能先取消,因为产生了许多误解
                            //SetHololensSynchronize(true);
                        }
                        else
                        {
                            waitingString = savehMsg.result.errorString;
                        }
                        break;

                    case LiveMessageConstant.BEV_MESSAGE_TYPE_RESPONSE_SPATIAL_MAPPING:
                        LiveMessageResponseSpatialMapping spatialMsg = msg as LiveMessageResponseSpatialMapping;
                        SetSpatialMapping(spatialMsg);
                        break;
                    }
                }

                // 判断是否已经不传输了
                if (lastSyncTime >= 0)
                {
                    if (Time.time - lastSyncTime > syncTimeoutInterval)
                    {
                        hololensStartSynchronize = false;
                    }
                }
            }

            if (useUDP && udpListener != null)
            {
                while (udpListener.UdpReceiveQueue.GetCount() > 0)
                {
                    byte[] messageBytes = udpListener.UdpReceiveQueue.Dequeue();
                    //Debug.Log(messageBytes.Length + "---" + messageBytes[0]);
                    LiveMessage msg = LiveMessageManager.ParseMessage(messageBytes);

                    if (msg == null)
                    {
                        continue;
                    }

                    switch (msg.type)
                    {
                    case LiveMessageConstant.BEV_MESSAGE_TYPE_SYNCHRONIZE_ALL:
                        LiveMessageSynchronizeAll syncMsg = msg as LiveMessageSynchronizeAll;
                        //Debug.Log("-->"+syncMsg.seq);
                        DealSyncMessage(syncMsg);

                        break;
                    }
                }
            }
        }