1. 程式人生 > >holotoolkit建立多個客戶端

holotoolkit建立多個客戶端

1, HoloToolkit/unity遠端實時傳輸視訊

      holotoolkit 客戶端與伺服器架構的建立

2 上面的部落格講述瞭如何建立客戶端和伺服器,但只能是一對一。

下面以多加一個客戶端為例來說。(a, b, c 三個,資料從a向b和c傳送。)

a 的工程中  (1) LocalCustomMessages.cs  多加一個Middle

public enum TestMessageID : byte
        {
            StringMessageID = MessageID.UserMessageIDStart,
            Middle,
            Max
        }

(2)LocalCustomMessages.cs 裡面SendMessage給兩個分別發

public void SendMessage()
		{
			// If we are connected to a session, broadcast our head info
			if (serverConnection != null && serverConnection.IsConnected())
			{
				// Create an outgoing network message to contain all the info we want to send
				NetworkOutMessage msg = CreateMessage((byte)TestMessageID.StringMessageID);
				NetworkOutMessage msgm = CreateMessage((byte)TestMessageID.Middle);

				////send video
//				byte[] currentColorFrameByteArray = LocalCamera.viewTexture.EncodeToJPG();
//
//				msg.Write(currentColorFrameByteArray.Length);
//				msg.WriteArray(currentColorFrameByteArray, (uint)(currentColorFrameByteArray.Length));
				///send there or more clients ---test
				AppendVector3(msg, Vector3.one);

				// Send the message as a broadcast, which will cause the server to forward it to all other users in the session.
				serverConnection.Broadcast(
					msg,
					MessagePriority.Immediate,
					MessageReliability.UnreliableSequenced,
					MessageChannel.Avatar);


				AppendVector3(msgm, 10*Vector3.one);

				// Send the message as a broadcast, which will cause the server to forward it to all other users in the session.
				serverConnection.Broadcast(
					msgm,
					MessagePriority.Immediate,
					MessageReliability.UnreliableSequenced,
					MessageChannel.Avatar);
			}
		}

(3)LocalCustomMessagesManager.cs  Start裡多加一行Middle

    private void Start()
        {
			LocalCustomMessages.Instance.MessageHandlers[LocalCustomMessages.TestMessageID.StringMessageID] = UpdateMessage;
			LocalCustomMessages.Instance.MessageHandlers[LocalCustomMessages.TestMessageID.Middle] = UpdateMessage;

            // SharingStage should be valid at this point, but we may not be connected.
            if (SharingStage.Instance.IsConnected)
            {
                Connected();
            }
            else
            {
                SharingStage.Instance.SharingManagerConnected += Connected;
            }
        }

b 客戶端工程 對應的加 Middle 即可

    /// </summary>
        public enum TestMessageID : byte
        {
            StringMessageID = MessageID.UserMessageIDStart,
            Middle
            Max
        }

c 客戶端工程

(1)加Middle enum

     public enum TestMessageID : byte
        {
            StringMessageID = MessageID.UserMessageIDStart,
			Middle,
            Max
        }

(2) 把指令碼中對應的StringMessageID 修改為Middle即可。

        public void SendMessage()
        {
            // If we are connected to a session, broadcast our head info
            if (serverConnection != null && serverConnection.IsConnected())
            {
                // Create an outgoing network message to contain all the info we want to send
				//2 michael
				NetworkOutMessage msg = CreateMessage((byte)TestMessageID.Middle);
                //NetworkOutMessage msg = CreateMessage((byte)TestMessageID.Middle);

                ////send video
                //byte[] currentColorFrameByteArray = cameraView.viewTexture.EncodeToJPG();

                //msg.Write(currentColorFrameByteArray.Length);
                //msg.WriteArray(currentColorFrameByteArray, (uint)(currentColorFrameByteArray.Length));

                // Send the message as a broadcast, which will cause the server to forward it to all other users in the session.
                serverConnection.Broadcast(
                    msg,
                    MessagePriority.Immediate,
                    MessageReliability.UnreliableSequenced,
                    MessageChannel.Avatar);
            }
        }
  private void Start()
        {
			//1 micheal
			//LocalCustomMessages.Instance.MessageHandlers[LocalCustomMessages.TestMessageID.StringMessageID] = UpdateMessage;
			LocalCustomMessages.Instance.MessageHandlers[LocalCustomMessages.TestMessageID.Middle] = UpdateMessage;

            // SharingStage should be valid at this point, but we may not be connected.
            if (SharingStage.Instance.IsConnected)
            {
                Connected();
            }
            else
            {
                SharingStage.Instance.SharingManagerConnected += Connected;
            }
        }