/// <summary> /// broadcasting the received message to all the clients on the channel /// </summary> /// <param name="command">the message to broadcast</param> public void BroadcastToClients(ServerClientCommunicationCommand command) { if (running) { new Task(() => { if (running) { string commandJson = command.ToJson(); foreach (IClientHandler clientHandler in clients) { try { clientHandler.WriteMessage(commandJson); } catch (Exception) { //if communication didn't succedd close clientHandler clientHandler.CloseHandler(); } } } }).Start(); } }
/// <summary> /// starting communication from client side with the server /// </summary> public void Start() { if (running) { new Task(() => { try { //this.clientsAndStreams.Add(client, stream string receivedString; while (this.running) { lock (reader) { int nameLength = reader.ReadInt32(); string name = Encoding.Default.GetString(reader.ReadBytes(nameLength)); int imageLength = reader.ReadInt32(); byte[] image = reader.ReadBytes(imageLength); string imageConvert = Convert.ToBase64String(image); //string imageConvert = Encoding.ASCII.GetString(image); string[] args = new string[2]; args[0] = name; args[1] = imageConvert; ServerClientCommunicationCommand command = new ServerClientCommunicationCommand() { CommId = CommandEnum.PhotoTransferCommand, Args = args }; MessageReceived?.Invoke(this, new MessageCommunicationEventArgs { Message = command.ToJson() }); } } } catch (Exception exc) { //if communication didn't succedd close clientHandler this.CloseHandler(); } }).Start(); } }