Sign in to follow this  
purri

Unity .Net server and chat channels

Recommended Posts

Im working on ARPG genre game with unity and i have been developing server by using vb.net 2010. Im running login , lobby and game sockets on ports 7111-7113 and each socket has own thread. example : When client connects login thread launch new thread and binds this new client data flow trought that.

here is picture to show logic behind my work :

[attachment=14457:TCPServer.jpg]
But now to the my question. How i create channels? I want 16 player limited channels to chat and 8 players to ingame channel.

Think about this: my client send data , packet structure is [ID,DATA] = [0x01 ,"Hello/Channel1"]. Server must check from array or collection where to send this data. We know that i want send it to clients in Channel1. I have thinking about creating multidimensional array OR collection, collection would be nice because i can acces those client's easily.

AND second question.

How i can store channels to collection? I know how to use collection, so im not asking help for that. But im thinking of multidimensional collection , which inculdes variables Object and String. Object stays one of connected client and string has channel name. 

could it work ? tell me ! icon_biggrin.gif


I need some example code. Please help me!
 

Share this post


Link to post
Share on other sites

A simple Dictionary of channel name to members should work just fine in the simple case.

Yeah i think so, but could you drop me whit some example code?  I need some logic smile.png

 

I write here something what i have thinking.. 

every time when client connects server must send channel name to join. That channel must have less than 16 players inside. So can i have dictionary which have 

 

1. Collection of playerclients so i can communicate with them

2. Channel name

3. number of user on that channel 

 

Can i do like :

Dim LobbySockets As New Collection(Of LobbyConnection,String,integer)  

( LobbyConnection presents new instance of our class ,was made when the client connected)

?

Edited by Miraz

Share this post


Link to post
Share on other sites
I would make the channel be a binary ID in front of the text, rather than a substring within the payload.
Thus, the data could be:

[0x1, 0x5, "Hello"]
This would say "message type 1 (text chat) on channel 5, data "Hello".

The code for receiving command type 1 would then dispatch to the right channel:
Dictionary<byte, Channel> channels = new Dictionary<int, Channel>();

void DispatchChat(byte[] data) {
  Channel chan;
  if (!channels.TryGetValue(data[1], out chan)) {
    // no such channel
    return;
  }
  chan.ChatMessage(data);
}
There are many other ways of slicing this (such as dispatching on channel first, command second, or using a different container, or targeting messages at "objects" where one kind of "object" is a "channel" or ...)

Also: One thread per user works for small numbers of users (a few hundred, say.) For good performance and resource utilization, though, you want to use BeginReceive()/BeginSend() or ReceiveAsync()/SendAsync() and let the OS do the thread pooling for you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this