GroupLab.Networking :: Dictionary Users

This example demonstrates how users can see other users connected to the Shared Dictionary using GroupLab.Networking.

Download the source code and executable

 Sections of this example:

  1. Setting up the form
  2. Adding code to Form1.cs
    1. Class Properties
    2. Connect / Disconnect
    3. SD Connected Event
    4. SD Closed Event
    5. Show Nickname List
    6. Receiving Notifications
  3. Adding code to Form2.cs
    1. Class Properties
    2. Connect
  4. What you will get

I. Setting up the form

1. Start a new C# Windows Application in Visual Studio and add a reference to GroupLab.Networking.dll.  For detailed instructions click here.

2. Add a RichTextBox and a Button to Form1 like in the image below:

       

3. Drag a Shared Dictionary and a Subscription from the toolbox to the form.

       

4. Add another windows form to the project. Drag two Labels, two TextBoxes and a Button to this form.

       


II. Adding code to Form1.cs

1 Class Properties: The main form needs to store the list of all connected users, for this we use a hashtable.  Also the nickname of the client needs to be stored in order to be placed in the Shared Dictionary once connected.

The following code needs to be added in the properties declaration section of Form1:

private string Nickname;
private Hashtable users;

2 Connect / Disconnect: The button will be used to connect and disconnect from the shared dictionary, toggling between the two functions. When connecting, a Connect dialog will ask the user for a nickname to use and a URL to connect to. 

The URL format is: tcp://<computer name or IP>:<port # / dictionary name>
For example, "tcp://localhost:hello" will connect to a shared dictionary named hello on the the local machine.

Two event handlers are added, SD_Connected and SD_Closed, to notify the application that it has successfully opened or closed a connection with the shared dictionary. 

Add the following code to the button1_Click event:

// method called when Connect/Disconnect button is clicked
private void button1_Click(object sender, System.EventArgs e)
{
    // if state of the butotn is Connect
    if(this.button1.Text.CompareTo("Connect")==0)
    {
        Form2 f2 = new Form2();
        f2.ShowDialog(this); // display the Select Nickname and Server dialog
        if(f2.Server==null) // if no server was entered
            return; // then do nothing
        if(f2.Nickname==null) // if no nickname was entered
            return; // then do nothing
   
        this.Nickname = f2.Nickname; // set the nickname for this instance of the application
        this.SD.Url = f2.Server; // set the url the shared dictionary should connect to
        this.SD.Connected += new EventHandler(SD_Connected);
        this.SD.Closed += new EventHandler(SD_Closed);
        this.SD.Open(); // and connect
    }
    // if state of the button is Disconnect
    if(this.button1.Text.CompareTo("Disconnect")==0)
    {
        this.SD.Close(); // close the connection
        this.users.Clear(); // empty the user list
        shownicklist(); // and redisplay it
    }
}

3. SD Connected Event: This event will fire when the application connects to the shared dictionary. The .transient attribute of the dictionary entry lets the dictionary know that when the client that set this entry disconnects, the entry should be deleted automatically. 

Add the following code to the SD_Connected event:

private void SD_Connected(object sender,System.EventArgs args)
{
    this.subscription1.BeginInit();
    this.subscription1.Pattern = "/users/*"; // subscribe to this dictionary path
    // set an event notification for that subscription

    this.subscription1.Notified+=new SubscriptionEventHandler(subscr_users_Notified);
    this.subscription1.EndInit();
 

    this.button1.Text = "Disconnect"; // toggle the button
    // set our info into the dictionary and make it .transient

    this.SD["/users/"+this.SD.Id.ToString()+".transient"] = this.SD.Id;
   
// set our nickname so other users can see we connected
    this.SD.Add("/users/"+this.SD.Id.ToString(),this.Nickname);
}


4. SD Closed Event: This event will fire when the application disconnects from the shared dictionary.   We give the user the possibility to reconnect or to open a connection to a new server by enabling the connect button. 

Add the following code to the SD_Closed event:

// event called when connection is closed or we get disconnected
private void SD_Closed(object sender,System.EventArgs args)
{
    this.users.Clear(); // empty the nick list
    shownicklist(); // show the empty list
    SD.Remove("*"); // clear the local copy of the dictionary
    this.button1.Text = "Connect"; // change the button text back to Connect
}

5. Show Nickname List: A hashtable is used to store the nicknames of all connected users. In order to display them, the hashtable has to be traversed.

For this, create the shownicklist method by adding the following code to Form1.cs:

// show the list of users connected
private void shownicklist()
{
    this.richTextBox1.Text = "";
    IDictionaryEnumerator en = users.GetEnumerator();
    while(en.MoveNext())
      this.richTextBox1.AppendText(en.Value.ToString()+"\r\n");
}

6. Receiving Notifications: When a user connects or disconnects, the application will be notified about it.  If a user connects he is added to the hashtable. If he disconnects he is removed.

Add the following code to the subscr_users_Notified event:

// event called when a user connects or disconnects
private void subscr_users_Notified(object sender,GroupLab.Networking.SubscriptionEventArgs args)
{
    // if a user connected
    if(SubscriptionNotification.Add == args.Reason)
    {
        // if the user is not already in the hashtable
        if(!users.Contains(args.Path.ToString()))
        {
            // add him to the hashtable
            users.Add(args.Path.ToString(),args.Entry.Value.ToString());
            shownicklist(); // and refresh the user list on the screen
        }
    }
    // if a user disconnects
    if(SubscriptionNotification.Remove == args.Reason)
    {
        if(users.Contains(args.Path.ToString())) // if the user is in the hashtable
        {
            users.Remove(args.Path.ToString()); // remove him
            shownicklist(); // and refresh the user list on the screen
        }
    }
}

II. Adding code to Form2

1 Class Properties: The Connect dialog needs to store the server url and nickname of the client wanting to connect to a remote Shared Dictionary. The following code needs to be added in the properties declaration section of Form2:

public string Server;
public string Nickname;

2 Connect button: Upon clicking the Connect button, the url and nickname are stored in the two properties declared above so that Form1 can access them.

// method called when the Connect button is pressed
private void button1_Click(object sender, System.EventArgs e)
{
    this.Server = this.textBox1.Text;
    this.Nickname = this.textBox2.Text;
    this.Close();
}


IV. What you will get

1.Open an instance of the application. Hit "Connect".

       

2. In the Select Nickname and Server dialog enter a nickname and a hostname:port or leave the default ones and press "Connect".

       

3. Open several other instances and connect them to the SAME hostname:port with different nicknames. As different instances connect, the list of users in each will be updated accordingly.