public static void SendMessage(string textMessage) { _serverParcel.Msg = textMessage; _serverParcel.TimeStamp = DateTime.Now; //update DB with the server message TCPServerDB.AddNewMessage(_serverParcel); SendParcel(_serverParcel); }
private static void cHandler_ParcelArrived(object sender, Parcel e) { if (e.Msg.Contains("***has connected***")) { //a special connection message OnClientConnected(new ClientConnectedArgs { Id = e.SenderID, UserName = e.UserName, ConnectionTime = e.TimeStamp }); } if (e.Msg.Contains("***HAS LEFT THE CHAT***")) { // a special disconnection message OnClientDisconnected(new ClientDisconnectedArgs { Id = e.SenderID, UserName = e.UserName, ConnectionTime = e.TimeStamp }); } if (e.Msg.Contains("***Checking")) //client checks whether the username is registered //such messsages are not sent to other clients { //extractUserName from the message string extractedUserName = e.Msg.Substring("***Checking".Length, e.Msg.IndexOf("***Name") - "***Checking".Length); //extract the name from the message string extractedName = e.Msg.Substring(e.Msg.IndexOf("***Name") + "***Name".Length, e.Msg.Length - "***Name".Length - e.Msg.IndexOf("***Name")); //query DB findByUserName if (TCPServerDB.FindByUserName(extractedUserName) != null) { //if UserName found, then send confirmation message to client SendMessage("***Checked" + extractedUserName + "***Name"); return; } //sent without a name - if (extractedName == string.Empty) { //if UserName not found then send NoSuchUser SendMessage("***nosuchuser"); return; } TCPServerDB.AddNewUser(extractedUserName, extractedName); //after the UserName and private name are registered, then send confirmation message to client SendMessage("***Checked" + extractedUserName + "***Name"); return; } SendParcel(e); OnServerParcelReceived(e); }
private static void OnServerParcelReceived(Parcel e) { TCPServerDB.AddNewMessage(e); EventHandler <Parcel> handler = ServerParcelReceived; if (handler != null) { handler(null, e); } }
private static void OnClientConnected(ClientConnectedArgs firstTimeConnection) { //update DB TCPServerDB.UpdateUserConnectionStatus(firstTimeConnection.UserName, true); EventHandler <ClientConnectedArgs> handler = ClientConnected; if (handler != null) { handler(null, firstTimeConnection); } }
private static void OnClientDisconnected(ClientDisconnectedArgs e) { //update DB TCPServerDB.UpdateUserConnectionStatus(e.UserName, false); ClientHandler ch; //trying to remove the disconnected user SocketConnections.TryGetValue(e.Id, out ch); if (ch != null) { ch.TokenSourceConnection.Cancel(); ch.Dispose(); SocketConnections.Remove(e.Id); } EventHandler <ClientDisconnectedArgs> handler = ClientDisconnected; if (handler != null) { handler(null, e); } }