Skip to content

Add Function to send messages to clients #3

@strangerattractor

Description

@strangerattractor

I found a method to send messages to clients here:
https://stackoverflow.com/questions/27021665/c-sharp-websocket-sending-message-back-to-client

And it works just fine with your server.
I only need to talk to one client, so I implemented it in very janky way like this:


public virtual void SendMessageToClient(string msg)
        {
            NetworkStream stream = connectedTcpClient.GetStream();
            Queue<string> que = new Queue<string>(msg.SplitInGroups(125));
            int len = que.Count;

            while (que.Count > 0)
            {
                var header = GetHeader(
                    que.Count > 1 ? false : true,
                    que.Count == len ? false : true
                );

                byte[] list = Encoding.UTF8.GetBytes(que.Dequeue());
                header = (header << 7) + list.Length;
                stream.Write(IntToByteArray((ushort)header), 0, 2);
                stream.Write(list, 0, list.Length);
            }
        }


        protected int GetHeader(bool finalFrame, bool contFrame)
        {
            int header = finalFrame ? 1 : 0;//fin: 0 = more frames, 1 = final frame
            header = (header << 1) + 0;//rsv1
            header = (header << 1) + 0;//rsv2
            header = (header << 1) + 0;//rsv3
            header = (header << 4) + (contFrame ? 0 : 1);//opcode : 0 = continuation frame, 1 = text
            header = (header << 1) + 0;//mask: server -> client = no mask

            return header;
        }


        protected byte[] IntToByteArray(ushort value)
        {
            var ary = BitConverter.GetBytes(value);
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(ary);
            }

            return ary;
        }
        
        /// use this method because smaller messsages
        private byte[] encodeMessage(string msg)
        {
            int msgLength = msg.Length;
            byte[] msgEncoded = new byte[msgLength + 5];
            int indexAt = 0;

            msgEncoded[0] = 0x81;
            indexAt++;

            if (msgLength <= 125)
            {
                msgEncoded[1] = (byte)msgLength;
                indexAt++;
            } else if (msgLength >= 126 && msgLength <= 65535)
            {
                msgEncoded[1] = 126;
                msgEncoded[2] = (byte)((byte)msgLength >> 8);
                msgEncoded[3] = (byte)msgLength;
                indexAt += 3;
            }

            for (int i = 0; i < msgLength; i++)
            {
                msgEncoded[indexAt + i] = (byte)msg[i];
            }
            return msgEncoded;
        }


    }
        /// ================= [ extension class ]==============>

    public static class XLExtensions
    {
        public static IEnumerable<string> SplitInGroups(this string original, int size)
        {
            var p = 0;
            var l = original.Length;
            while (l - p > size)
            {
                yield return original.Substring(p, size);
                p += size;
            }
            yield return original.Substring(p);
        }
    }

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions