Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.
Options

How to use the custom messages class

edited February 2018 in Questions And Answers

Like a few Others on this forum, I'm trying to send data between multiple Hololenses using the custom messages class (CustomMessages.cs).

Since there is no step by step documentation on how to use this class I tried to follow the steps posted in this topic:

https://forums.hololens.com/discussion/3274/sending-a-game-object-name-through-custom-messages-in-sharing-of-holograms

My Hololenses do connect to the sharing service and join the same session but it seems that no communication is happening.

These are my scripts:

-Added to the custom Messages script (I invoke the SendGameObj method by pressing a button ingame):

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

and:

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

                //Appending a gameobj to the message
                msg.Write(obj.name);

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

                Debug.Log ("message sent");
            }
        }

-Separate script where I try to receive the sent messages:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using  HoloToolkit.Sharing.Tests;
using HoloToolkit.Sharing;

public class TestMessage : MonoBehaviour {

    public GameObject testObject; //some random test object who's color will be changed


    // Use this for initialization
    void Start () {
        CustomMessages.Instance.MessageHandlers[CustomMessages.TestMessageID.Max] = this.OnGameObjIdRcv;
    }

    void OnGameObjIdRcv(NetworkInMessage msg)
    {
        msg.ReadInt64();

        string objname_in_msg = msg.ReadString();

        print ("object name: " + objname_in_msg);
        testObject.GetComponent<MeshRenderer> ().material.color = Color.red; //make object red to see whether this method triggers on Hololens
    }

    public void LocalPrint () {
        print ("local  button test");
    }

}

Best Answer

Answers

  • Options

    @Paul_Plokko ,

    It looks like you send a string, but then try to read a long and then a string. Get rid of msg.ReadInt64() and see what happens.

    James

    James Ashley
    VS 2017 v5.3.3, Unity 2017.3.0f3, MRTK 2017.1.2, W10 17063
    Microsoft MVP, Freelance HoloLens/MR Developer
    www.imaginativeuniversal.com

  • Options

    @dbarrett said:
    You should take a look at the RemoteHeadManager script as it utilizes the CustomMessages script.

    You can also take a look at this video where Stephen goes over the Custom messages class in a MRTK live session and I believe he also expands upon it.

    https://www.youtube.com/watch?v=Zm_c02y4pdw

    Thanks for the video! It enabled me to understand the custom messages script messages much better. My problem was that I was using the 'Max' enum message type, whereas i should create my own message type.

    @james_ashley said:
    @Paul_Plokko ,

    It looks like you send a string, but then try to read a long and then a string. Get rid of msg.ReadInt64() and see what happens.

    James

    Reading the long is actually necessary in this case because it reads the User Id which is sent in the message.

  • Options

    @Paul_Plokko ,

    But you aren't sending a userid in your SendGameObj() method above. Do you need to send it (hence causing casting errors in the receiver?) or is this not actually then send method you use in your code?

    James Ashley
    VS 2017 v5.3.3, Unity 2017.3.0f3, MRTK 2017.1.2, W10 17063
    Microsoft MVP, Freelance HoloLens/MR Developer
    www.imaginativeuniversal.com

  • Options

    The custom messages class itself adds the user id when creating a message:

            private NetworkOutMessage CreateMessage(byte messageType)
            {
                NetworkOutMessage msg = serverConnection.CreateMessage(messageType);
                msg.Write(messageType);
                // Add the local userID so that the remote clients know whose message they are receiving
                msg.Write(LocalUserID);
                return msg;
            }
    
Sign In or Register to comment.