Skip to content
This repository has been archived by the owner on May 12, 2020. It is now read-only.

Examples? #1

Open
shllybkwrm opened this issue Feb 15, 2018 · 10 comments
Open

Examples? #1

shllybkwrm opened this issue Feb 15, 2018 · 10 comments

Comments

@shllybkwrm
Copy link

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
Copy link
Owner

DynaSpan commented Feb 26, 2018

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
Copy link
Author

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
Copy link
Owner

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
Copy link

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
Copy link
Owner

DynaSpan commented Aug 27, 2018

@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
Copy link

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
Copy link
Owner

DynaSpan commented Aug 28, 2018

@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
Copy link

@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
Copy link
Owner

DynaSpan commented Sep 3, 2018

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
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants