Now I am working on Websockets, I am new to this, I can finally send a message of 126 bytes, but I need to send longer messages, but when I try to close the connection, my code is:
public void sendMessage(Stream stream, string message) { try { List<byte> lb = new List<byte>(); string aux = message; bool flagStart = false; int size; while (message.Length > _maxLengthMessage) { lb = new List<byte>(); // I cut the mesasge in smaller pieces to send message = aux.Substring(0, _maxLengthMessage); aux = aux.Substring(_maxLengthMessage); if (!flagStart) { // In doc of Websockets i sign this piece: not the end, text lb.Add(0x01); flagStart = !flagStart; } else { // In doc of Websockets i sign this piece: not the end, continuation lb.Add(0x00); } size = message.Length; lb.Add((byte)size); lb.AddRange(Encoding.UTF8.GetBytes(message)); stream.Write(lb.ToArray(), 0, size + 2); } lb = new List<byte>(); if (!flagStart) { // If is this the only message we mark with: end of message, text lb.Add(0x81); flagStart = !flagStart; } else { //else Is the end of the message but is the continuation frame lb.Add(0x80); } size = aux.Length; lb.Add((byte)size); lb.AddRange(Encoding.UTF8.GetBytes(aux)); //lb.AddRange(Encoding.UTF8.GetBytes(i.ToString())); stream.Write(lb.ToArray(), 0, size+2); } catch (Exception ex) { throw ex; } }
I don’t know if I work very well, but I need help, it is urgent, and I don’t know what to do, I search on the Internet, but I always find the same answer. "Go to The WebSocket Protocol" I did this and nothing, maybe I do not understand very well, so I need your help.
Thanks.
ralph
source share