alvyxaz / barebones-masterserver

Master Server framework for Unity
475 stars 106 forks source link

Sending messages between client to server, use MSF to find out which client to reply to, or Unet? #192

Open MostHated opened 6 years ago

MostHated commented 6 years ago

Hello all, I am trying to figure out the best way to go about sending some MessagePackCSharp serialized byte[] between client and server. I want them to be as quick and as small as possible so I figure it is best to try and send them directly through Unet from client to server and try to use MSF as little as possible as the messages pertain only to deterministic actions for the game. I am trying to figure out how to make sure the server knows who to send the message to / reply to.

The messages are all "Operation requests" to the server and "Operations Replies" or "Event Data" from the server to the client. The "Operation request" would be essentially a [Command] and an Event is a [ClientRPC]. What I am trying to figure out is how to make sure to apply the right incoming message to the correct client on the server side, and make sure to send back the response or event to the right client(s). So say I am Client A and I want to cast a spell, so I send an operation request to the server called CastSpell(), I have the process down on the client and server side side up to the point of serialization with MessagePack but need to know how to make sure the server knows its from Client A and to apply it to Client A.

I know that MSF has BasePeer / IPeer but then there is also UnetMfsPlayer (I believe is the name), do I grab one of those and send the message based on that somehow?

If anyone has or knows where I can find an example I could look at I would really appreciate it. The examples included with MSF I don't think would work since client and server are pretty much the same, I am using a separate client and server build.

Thanks!

ghost commented 6 years ago

You can build your own server and clients through the implementations of IClientSocket and IServerSocket. Since these implementations already have a way of knowing who is sending, all you need to do is find who is not sending / requesting something.

That can be done by storing the connections in a list or dictionary in the IServerSocket class. Each time someone sends something, you act on his peer (handle the message and respond) and get the rest of the peers by iterating through the dictionary or list, where you get everyone that isn't that peer. Then, you broadcast to them the request that you handled.