// (4) Send The Recieved Data to All Clients in The Room private static void OnRecievedData(IAsyncResult ar) { SocketCoderClient client = (SocketCoderClient)ar.AsyncState; byte[] aryRet = client.GetRecievedData(ar); if (aryRet.Length < 1) { // client.Sock.RemoteEndPoint - "has left the room" client.ReadOnlySocket.Shutdown(SocketShutdown.Both); client.ReadOnlySocket.Close(); // ClientsList.Remove(client); return; } try { MemoryStream msgStream = new MemoryStream(); ClientCommand msgCommand = new ClientCommand(); msgStream.Write(aryRet, 0, aryRet.Length); BinaryFormatter deserializer = new BinaryFormatter(); msgStream.Position = 0; msgCommand = (ClientCommand)deserializer.Deserialize(msgStream); switch (msgCommand.MessageType) { case ClientCommand.msgType.JoinRoom: ArrayList RoomMembers; if (Clients.ContainsKey(msgCommand.ChatRoomID)) { RoomMembers = Clients[msgCommand.ChatRoomID]; client.username = msgCommand.Sender; RoomMembers.Add(client); Clients[msgCommand.ChatRoomID] = RoomMembers; } else { RoomMembers = new ArrayList(); client.username = msgCommand.Sender; RoomMembers.Add(client); Clients.Add(msgCommand.ChatRoomID, RoomMembers); } try { Member member = new Member(); member.AddNew(); member.ChatRoomID = msgCommand.ChatRoomID; member.MemberID = msgCommand.Sender; member.Save(); } catch (Exception ex) { } ArrayList Members1 = Clients[msgCommand.ChatRoomID]; for (int i = 0; i < Members1.Count; i++) { SocketCoderClient Current = (SocketCoderClient)Members1[i]; Current.ReadOnlySocket.Send(aryRet); } //OnLogText(null, new EventArgs(), "user : "******" has joined room : " + msgCommand.ChatRoomID.ToString()); break; case ClientCommand.msgType.LeaveRoom: try { Member memberToLeave = new Member(); memberToLeave.LoadByPrimaryKey(msgCommand.Sender, msgCommand.ChatRoomID); memberToLeave.MarkAsDeleted(); memberToLeave.Save(); } catch (Exception ex) { } ArrayList Members2 = Clients[msgCommand.ChatRoomID]; for (int i = 0; i < Members2.Count; i++) { SocketCoderClient Current = (SocketCoderClient)Members2[i]; if (Current.username != msgCommand.Sender) Current.ReadOnlySocket.Send(aryRet); } ArrayList MemberToRemove = Clients[msgCommand.ChatRoomID]; for (int i = 0; i < MemberToRemove.Count; i++) { SocketCoderClient Current = (SocketCoderClient)MemberToRemove[i]; if (Current.username != msgCommand.Sender) continue; else { MemberToRemove.Remove(Current); Current.ReadOnlySocket.Shutdown(SocketShutdown.Both); // prevent setup recieve call back for removed client return; // break; } } break; case ClientCommand.msgType.Message: ArrayList Members = Clients[msgCommand.ChatRoomID]; for (int i = 0; i < Members.Count; i++) { SocketCoderClient Current = (SocketCoderClient)Members[i]; Current.ReadOnlySocket.Send(aryRet); } break; default: break; } } catch (Exception ex) { MessageBox.Show("OnRecievedData - " + ex.Message); } //client.Send(msgStream.ToArray()); /* foreach (SocketCoderClient clientSend in ClientsList) { try { clientSend.ReadOnlySocket.Send(aryRet); } catch { clientSend.ReadOnlySocket.Close(); ClientsList.Remove(client); return; } }*/ client.SetupRecieveCallback(); }
private void uiRichTextBoxMsg_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { if (e.Shift) { uiRichTextBoxMsg.Rtf += Environment.NewLine; } else { MemoryStream msgStream = new MemoryStream(); ClientCommand msgCommand = new ClientCommand(); msgCommand.ChatRoomID = ChatRoomID; msgCommand.CategoryId = CategoryID; msgCommand.SubCategoryId = SubCategoryID; msgCommand.Sender = CurrentUser.Client.Name; msgCommand.Message = uiRichTextBoxMsg.Text; msgCommand.MessageType = ClientCommand.msgType.Message; BinaryFormatter serializer = new BinaryFormatter(); serializer.Serialize(msgStream, msgCommand); msgStream.Seek(0, SeekOrigin.Begin); uiRichTextBoxMsg.Text = msgCommand.Sender + " says :\n\r" + uiRichTextBoxMsg.Text; uiRichTextBoxMsg.Select(0, uiRichTextBoxMsg.Text.IndexOf(" says :") + 7); uiRichTextBoxMsg.SelectionFont = new System.Drawing.Font(uiRichTextBoxMsg.SelectionFont.FontFamily, uiRichTextBoxMsg.Font.Size, FontStyle.Bold); foreach (var item in CurrentUser.Emotions) { int _index; if ((_index = uiRichTextBoxMsg.Find(item.Key)) > -1) { uiRichTextBoxMsg.Select(_index, item.Key.Length); InsertImage(new Bitmap(typeof(uiFormMain), item.Value), uiRichTextBoxMsg); } } uiRichTextBoxHistory.SelectedRtf = uiRichTextBoxMsg.Rtf; uiRichTextBoxMsg.Clear(); uiRichTextBoxMsg.Text = string.Empty; ClientSocket.Send(msgStream.ToArray()); } } }
public void OnRecievedData(IAsyncResult ar) { Socket sock = (Socket)ar.AsyncState; if (!uiRichTextBoxHistory.InvokeRequired) { try { int nBytesRec = sock.EndReceive(ar); if (nBytesRec > 0) { //string sRecieved = Encoding.Unicode.GetString(buffer, 0, nBytesRec); if (buffer == null) buffer = new byte[nBytesRec]; MemoryStream msgStream = new MemoryStream(); ClientCommand msgCommand = new ClientCommand(); msgStream.Write(buffer, 0, nBytesRec); BinaryFormatter deserializer = new BinaryFormatter(); msgStream.Position = 0; msgCommand = (ClientCommand)deserializer.Deserialize(msgStream); // OnAddMessage(sRecieved); // View Messages here if (msgCommand.MessageType == ClientCommand.msgType.JoinRoom || msgCommand.MessageType == ClientCommand.msgType.LeaveRoom) { LoadMembers(); } else if (msgCommand.Sender != CurrentUser.Client.Name) { RichTextBox temp = new RichTextBox(); temp.Text = msgCommand.Sender + " says : \n\r" + msgCommand.Message; temp.Select(0, temp.Text.IndexOf(" says :") + 7); temp.SelectionFont = new System.Drawing.Font(temp.SelectionFont.FontFamily, temp.Font.Size, FontStyle.Bold); foreach (var item in CurrentUser.Emotions) { int _index; if ((_index = temp.Find(item.Key)) > -1) { temp.Select(_index, item.Key.Length); InsertImage(new Bitmap(this.Parent.Parent.GetType(), item.Value), temp); } } uiRichTextBoxHistory.SelectedRtf = temp.Rtf; temp.Dispose(); } SetupRecieveCallback(sock); } else { sock.Shutdown(SocketShutdown.Both); sock.Close(); } } catch (Exception) { } } else { HandleOnRecievedDataCallBack b = new HandleOnRecievedDataCallBack(OnRecievedData); this.Invoke(b, new object[] { ar }); } }
void Disconncet() { try { if (ClientSocket != null & ClientSocket.Connected) { //Send_MSG("has left the room"); MemoryStream msgStream = new MemoryStream(); ClientCommand msgCommand = new ClientCommand(); msgCommand.ChatRoomID = ChatRoomID; msgCommand.CategoryId = CategoryID; msgCommand.SubCategoryId = SubCategoryID; msgCommand.Sender = CurrentUser.Client.Name; msgCommand.Message = ""; msgCommand.MessageType = ClientCommand.msgType.LeaveRoom; BinaryFormatter serializer = new BinaryFormatter(); serializer.Serialize(msgStream, msgCommand); msgStream.Seek(0, SeekOrigin.Begin); ClientSocket.Send(msgStream.ToArray()); //ClientSocket.Close(); } } catch (Exception) { } }
public void OnConnect(IAsyncResult ar) { Socket sock = (Socket)ar.AsyncState; try { if (sock.Connected) { SetupRecieveCallback(sock); MemoryStream msgStream = new MemoryStream(); ClientCommand msgCommand = new ClientCommand(); msgCommand.ChatRoomID = ChatRoomID; msgCommand.CategoryId = CategoryID; msgCommand.SubCategoryId = SubCategoryID; msgCommand.Sender = CurrentUser.Client.Name; msgCommand.Message = ""; msgCommand.MessageType = ClientCommand.msgType.JoinRoom; BinaryFormatter serializer = new BinaryFormatter(); serializer.Serialize(msgStream, msgCommand); msgStream.Seek(0, SeekOrigin.Begin); sock.Send(msgStream.ToArray()); } else { Disconncet(); MessageBox.Show("Connection Failed"); } } catch (Exception) { } }