DynaSpan / MixedRealityNetworking

UDP networking library for Unity in combination with UWP and/or Hololens
MIT License
32 stars 18 forks source link

Examples? #1

Open shllybkwrm opened 6 years ago

shllybkwrm commented 6 years ago

Hi, I was just wondering whether you would be able to post a simple usage example? Perhaps some code that sends a simple message and works on all three system types. Thanks!

DynaSpan commented 6 years ago

Hi,

I will post them ASAP. But it's basically this:

Setup

SocketClientManager.Host = host;
SocketClientManager.Port = port;

// Subscribing on different messages
SocketClientManager.Subscribe((byte)MessageType.FACE, FaceDataReceived);
SocketClientManager.Subscribe((byte)MessageType.VOICE, VoiceDataReceived);

SocketClientManager.Connect();

Callbacks

private void FaceDataReceived(NetworkMessage nm)
{
    // parse the network message here
}

Sending messages

NetworkMessage nm = new NetworkMessage((byte)MessageType.SYNC);
nm.Write(ConnectToServer.stopwatch.ElapsedMilliseconds);

SocketClientManager.SendMessage(nm);
shllybkwrm commented 6 years ago

Thank you very much! I have some of this implemented, but will incorporate the rest and let you know with any issues. I am trying to communicate with a server not using this library, so I think my main issues is that I'm not sure how to incorporate the message IDs yet.

DynaSpan commented 6 years ago

Yeah, you could just remove it from of the classes, should be 5 minutes work I guess. I just pushed this on GitHub so other people could easily adapt it to fit in their own project.

chinmay-bhat commented 6 years ago

Hey, I was just wondering if you could help me out with the above code.

NetworkMessage nm = new NetworkMessage((byte)MessageType.SYNC);

I don't understand how the message ID's are supposed to be used. Also, can you please post a simple example of how to setup communication that sends some messages across the Unity client and UWP client?

DynaSpan commented 6 years ago

@chinmayabhat the message ID's are a byte enum. It's because in my use case I had to sent different types of messages over the same socket. So with this message ID, I know which kind of message I receive and how to process it.

You can find some example code above, I unfortunately haven't had the time yet to setup a correctly working demo (yeah I know it's six months ago :( )

chinmay-bhat commented 6 years ago

Hey, thanks for the quick reply. I am unable to understand the receiving part of the script. Is it possible for you to post a sample script for sending and receiving messages? Thanks a lot!

DynaSpan commented 6 years ago

@chinmayabhat if you check the code example above, you will see that you can 'subscribe' on a message ID. This means, that whatever method you bind to it, will get called when a message gets received with that ID. This method can then parse the message.

So: if I do this: SocketClientManager.Subscribe((byte)MessageType.FACE, FaceDataReceived);

All messages with message type/ID face will be sent to the FaceDataReceived method (with the NetworkMessage as parameter).

chinmay-bhat commented 6 years ago

@DynaSpan Thanks a lot! I was able to setup a connection between Unity PC application and HoloLens UWP app.

To parse the message, do I need to use Read() in NetworkMessage.cs? Or is there an other way? Because when I send a message, the callback is called, but no data is transmitted.

DynaSpan commented 6 years ago

Depends on what kind of values you put in the message. The message gets send as a byte array and the NetworkMessage class supports reading integers, floats, longs and bytes. You should be able to easily extend this functionality (readChar() for example). This is just a barebone framework/proof-of-concept to show how you can setup networking between Hololens and Unity.

chinmay-bhat commented 6 years ago

Thanks @DynaSpan. I'll let you know if I have any further issues. You have been very helpful. :)