Search code examples
c#collaborative-editing

Collaborative text editor


I've got a really annoying problem: I'm working on a collaborative text editor in C# an i want when that every client from the list to edit an shared textbox,but i'm getting stucked at characters from keyboard conversions. Here is the code for the keyDown Event:

private void downKey(object sender, KeyEventArgs e)
    {
        try
        {
            byte[] data;

            switch (e.KeyCode)
            {
                case Keys.Enter: data = Encoding.UTF8.GetBytes("enter");
                    break;
                case Keys.Back: data = Encoding.UTF8.GetBytes("backspace");
                    break;
                case Keys.Space: data = Encoding.UTF8.GetBytes("space");
                    break;
                default:


                        data = Encoding.UTF8.GetBytes(e.KeyCode.ToString().ToLower());


                    break;
            }

            TcpClient.GetStream().Write(data, 0, data.Length);

        }
        catch (SocketException se)
        {
            MessageBox.Show(se.Message);
        }
    }

Now every client from the list of clients connected to the application should receive the byte array with the input of the textBox.So on "OnDataReceived" callback of everyclient in convert this byte array to a string and try to update via a crossThreadDelegate the textbox of every client.

    public void OnDataReceived(IAsyncResult asyn)
    {
        try
        {
            TcpSpecificPacket theSockId = (TcpSpecificPacket)asyn.AsyncState;
            int dataReceivedLength = theSockId.thisClient.GetStream().EndRead(asyn);
            if (dataReceivedLength != 0)
            {
               textBoxArrived.Invoke(changeCrossTextBox, new object[] {Encoding.UTF8.GetString(theSockId.dataBuffer, 0, dataReceivedLength)});

               WaitForData();
            }
        }
        catch (IOException)
        {

        }

And now the the definition of the crossThread delegate :

    private void changeCrossTextBox(string mesaj)
    {

        if (mesaj.Equals("enter"))
        {
            textBoxArrived.Text += Environment.NewLine;
        }
        else if (mesaj.Equals("space"))
        {
            textBoxArrived.Text += " ";

        }
        else if (mesaj.Equals("backspace"))
        {
            textBoxArrived.Text = textBoxArrived.Text.Substring(0, textBoxArrived.Text.Length - 1);
        }
        else
        {
            textBoxArrived.Text += mesaj;
        }
    }

But my real problem is next: i cannot identify well the escape characters because i sent a byte array,i convert it to a string and then i try to update the textBox of every Client with that string. For example when i press shift key the string in all TextBox of client will be "shiftKey".Can you tell me please how to deal with these escape characters?Is there a method to send and update in ASCII.


Solution

  • I'm not 100% sure what your question is asking - but if you wish to filter a string to contain non-control chars, you can use this - if you change your code to use the OnKeyPress event, instead of OnKeyDown:

    if(!char.IsControl(e.KeyChar))
    {
        //Do Stuffz
    }
    

    There is lots of stuff to test keycodes on the 'char' object you can use.

    char.IsLetterOrDigit, char.IsPunctuation .. etc