Search Unity

.Net socket server and chat channels

Discussion in 'Multiplayer' started by Mirace, Mar 24, 2013.

  1. Mirace

    Mirace

    Joined:
    Nov 29, 2010
    Posts:
    481
    Im working on ARPG genre game 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 :
    $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 ! :D


    I need some example code. Please help me :)
     
    Last edited: Mar 25, 2013
  2. Mirace

    Mirace

    Joined:
    Nov 29, 2010
    Posts:
    481
  3. Mirace

    Mirace

    Joined:
    Nov 29, 2010
    Posts:
    481
    Still looking for answer :)
     
  4. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    Hmm... not entirely sure what your problem is. As a suggestion, consider creating a room class like so:

    Code (csharp):
    1.  
    2. //Written off the top of my head.
    3. //Rough Illustration only
    4.  
    5. public class Room {
    6.  
    7.  
    8.     public static List <Room> rooms = new List <Room>();
    9.  
    10.  
    11.     public string Name = "";
    12.     public List <User> users = new List <User>();
    13.  
    14.  
    15.     object _lock;
    16.  
    17.     public static Room GetOrAdd(string name) {
    18.         lock(_lock) {
    19.             var room = rooms.FirstOrDefault(r = > r.Name == name)
    20.             if (room == null) rooms.Add(room = new Room {
    21.                     Name = name
    22.                 });
    23.             return room;
    24.         }
    25.     }
    26.  
    27.  
    28.     public bool Join(User user) {
    29.         lock(_lock)
    30.             if (users.Count > 15) return false;
    31.             else users.Add(user);
    32.         Broadcast(new Message("User Joined: " + user.Name));
    33.         return true;
    34.     }
    35.  
    36.  
    37.     public void Leave(User user) {
    38.         lock(_lock)
    39.             if (!users.Contains(user)) return;
    40.             else users.Remove(user);
    41.         Broadcast(new Message("User Left: " + user.Name));
    42.         return true;
    43.     }
    44.  
    45.     public void Broadcast(Message m) {
    46.         lock(_lock)
    47.             foreach(var u in Users) user.Send(m);
    48.     }
    49. }
    50.  
     
  5. Mirace

    Mirace

    Joined:
    Nov 29, 2010
    Posts:
    481
    if (users.Count > 15) return false;

    What happens users after that? As i see it just returns false, it should create new room with limit of 16 players. Thanks for code, i still need to figure this by myself because currently that code just handles one room, right?
     
  6. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    Yeah, it can do whatever you want it to do - this your code! In the example given I simply assumed each room had a unique string array, and only 16 people could be in a room [though it supports a huge number of rooms - limited only by memory].
     
  7. Mirace

    Mirace

    Joined:
    Nov 29, 2010
    Posts:
    481
    Okey but how you gonna send data to these 16 players in room only ? i dont understand :/
     
  8. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    Then I probably suggest you look rethink what you're trying to do. Building multi-threaded servers isn't easy and if you can't figure it out few are going to be able to help you. Maybe muck around with Photon cloud - implementing chat in that system is fairly easy.

    However, to answer the question, I'd assume each player knows what room he is in, so can send his message to said room. That room then knows which players are in it [the list of users] and will loop through them all and send each one the message.
     
    Last edited: Apr 9, 2013
  9. Mirace

    Mirace

    Joined:
    Nov 29, 2010
    Posts:
    481
    Thanks for answer.
    Sorry but i was bit confused :) My project is kinda big.

    So, i have trying to translate this code to vb.net
    Code (csharp):
    1.  
    2. Public Class Room
    3.    
    4.     Public Shared rooms As List(Of Room) = New List(Of Room)
    5.    
    6.     Public Name As String = ""
    7.    
    8.     Public users As List(Of User) = New List(Of User)
    9.    
    10.    
    11.     Public Shared Function GetOrAdd(ByVal name As String) As Room
    12.         Dim room As var = rooms.FirstOrDefault(() => {  }, (r.Name = name))
    13.         If (room Is Nothing) Then
    14.             rooms.Add(room=newRoom{Name=nameUnknown)
    15.         End If
    16.         Return room
    17.     End Function
    18.    
    19.     Public Function Join(ByVal user As User) As Boolean
    20.         If (users.Count > 15) Then
    21.             Return false
    22.         Else
    23.             users.Add(user)
    24.         End If
    25.         Broadcast(New Message(("User Joined: " + user.Name)))
    26.         Return true
    27.     End Function
    28.    
    29.     Public Sub Leave(ByVal user As User)
    30.         If Not users.Contains(user) Then
    31.             Return
    32.         Else
    33.             users.Remove(user)
    34.         End If
    35.         Broadcast(New Message(("User Left: " + user.Name)))
    36.         Return true
    37.     End Sub
    38.    
    39.     Public Sub Broadcast(ByVal m As Message)
    40.         For Each u As var In Users
    41.             user.Send(m)
    42.         Next
    43.     End Sub
    44. End Class
    45.  
    And im going to build this class but im gonna modify it a little. I will post here as soon i get some stuff up and running.
     
  10. Mirace

    Mirace

    Joined:
    Nov 29, 2010
    Posts:
    481
    Npfs3000 can you check your code, is line " var room = rooms.FirstOrDefault(r = > r.Name == name)" written correctly? Im not familiar with c#
     
  11. Mirace

    Mirace

    Joined:
    Nov 29, 2010
    Posts:
    481
    Nevermind anymore. Thanks for class got it working atlast.