Advertisement
Guest User

Untitled

a guest
Apr 27th, 2012
1,749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. Send larger messages than 126 bytes websockets in C#
  2. public void sendMessage(Stream stream, string message)
  3. {
  4. try
  5. {
  6. List<byte> lb = new List<byte>();
  7. string aux = message;
  8. bool flagStart = false;
  9. int size;
  10. while (message.Length > _maxLengthMessage)
  11. {
  12. lb = new List<byte>();
  13. // I cut the mesasge in smaller pieces to send
  14. message = aux.Substring(0, _maxLengthMessage);
  15. aux = aux.Substring(_maxLengthMessage);
  16. if (!flagStart)
  17. {
  18. // In doc of Websockets i sign this piece: not the end, text
  19. lb.Add(0x01);
  20. flagStart = !flagStart;
  21. }
  22. else
  23. {
  24. // In doc of Websockets i sign this piece: not the end, continuation
  25. lb.Add(0x00);
  26. }
  27. size = message.Length;
  28.  
  29. lb.Add((byte)size);
  30. lb.AddRange(Encoding.UTF8.GetBytes(message));
  31. stream.Write(lb.ToArray(), 0, size + 2);
  32. }
  33. lb = new List<byte>();
  34. if (!flagStart)
  35. {
  36. // If is this the only message we mark with: end of message, text
  37. lb.Add(0x81);
  38. flagStart = !flagStart;
  39. }
  40. else
  41. {
  42. //else Is the end of the message but is the continuation frame
  43. lb.Add(0x80);
  44. }
  45. size = aux.Length;
  46.  
  47. lb.Add((byte)size);
  48. lb.AddRange(Encoding.UTF8.GetBytes(aux));
  49. //lb.AddRange(Encoding.UTF8.GetBytes(i.ToString()));
  50. stream.Write(lb.ToArray(), 0, size+2);
  51.  
  52. }
  53. catch (Exception ex)
  54. {
  55. throw ex;
  56. }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement