Join to ChatRoom:
Next coming the client, how client connect to server and user login to the chat room. To joining to chat room the below codes are executes
private void JoinToChatRoom()
{
if (chan == null && txtName.Text.Trim().Length != 0)
{
chan = new TcpChannel();
ChannelServices.RegisterChannel(chan,false);
// Create an instance of the remote object
objChatWin = new frmChatWin();
objChatWin.remoteObj = (SampleObject)Activator.GetObject(typeof(RemoteBase.SampleObject), txtServerAdd.Text);
if (!objChatWin.remoteObj.JoinToChatRoom(txtName.Text))
{
MessageBox.Show(txtName.Text+ " already joined, please try with different name");
ChannelServices.UnregisterChannel(chan);
chan = null;
objChatWin.Dispose();
return;
}
objChatWin.key = objChatWin.remoteObj.CurrentKeyNo();
objChatWin.yourName= txtName.Text;
this.Hide();
objChatWin.Show();
}
}
Here from user client application takes a name and check to server is the name available or not. If name is available then user gets the ‘CurrentKeyNo’ of server, it is the number of last chat message (how the key generated, describe in later) and open the ChatRoom window.
If user name is already taken by other user, then application asks to user for different name.
In server side to join a user in chat server “JoinToChatRoom()” method invokes, lets see what happens within the method –
public bool JoinToChatRoom(string name)
{
if (alOnlineUser.IndexOf(name) > -1)
return false;
else
{
alOnlineUser.Add(name);
SendMsgToSvr(name + " has joined into chat room.");
return true;
}
}
Here is user can successfully logged in to server then his name is added in a user collection, ‘alOnlineUser’ is it an ArrayList type object.
Read Next à
Read Next à
No comments:
Post a Comment