2012-02-20 48 views
0

現在我正在處理Websockets,我是新的,我終於可以發送126字節的消息,但我需要發送更長的消息,但是當我嘗試yhe連接時自動關閉,我的代碼是:在C#中發送比126字節的websockets更大的消息

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; 
     } 

    } 

我不知道如果我做得很好,但我需要幫助,這是迫切的,我不知道該怎麼做,我搜索互聯網,但我對總是找到相同的答案「轉到WebSocket協議」我做到了,沒有任何事,也許我不是很瞭解,所以我需要你的幫助。

謝謝。

+0

附子嗨,你能不能把你的更正後的代碼? – Novalis 2012-09-12 11:18:11

回答

2

您的編寫消息長度的代碼需要擴展。數據框圖the protocol spec中的擴展有效載荷顯示缺少的內容。

對於最多125個字節的消息,您的代碼是正確的。
對於消息> 125但是< = 65536字節,您需要寫入3個字節 - 第一個字節是126;以下2個字節給出消息長度。對於> 65536字節的消息,您需要寫入9個字節 - 第一個字節是127;以下8個字節給出消息長度。

+0

謝謝,這是工作! ^^ – ralph 2012-02-20 11:02:31

1

燁,你必須創建正確的框架,這裏是方法:

static private byte[] CreateFrame(string message, MessageType messageType = MessageType.Text, bool messageContinues = false) 
    { 
     byte b1 = 0; 
     byte b2 = 0; 

     switch (messageType) 
     { 
      case MessageType.Continuos: 
       b1 = 0; 
       break; 
      case MessageType.Text: 
       b1 = 1; 
       break; 
      case MessageType.Binary: 
       b1 = 2; 
       break; 
      case MessageType.Close: 
       b1 = 8; 
       break; 
      case MessageType.Ping: 
       b1 = 9; 
       break; 
      case MessageType.Pong: 
       b1 = 10; 
       break; 
     } 

     b1 = (byte)(b1 + 128); // set FIN bit to 1 

     byte[] messageBytes = Encoding.UTF8.GetBytes(message); 

     if (messageBytes.Length < 126) 
     { 
      b2 = (byte)messageBytes.Length; 
     } 
     else 
     { 
      if (messageBytes.Length < Math.Pow(2,16)-1) 
      {     
       b2 = 126; 

      } 
      else 
      { 
       b2 = 127; 
      } 

     } 

     byte[] frame = null; 

     if(b2 < 126) 
     { 
      frame = new byte[messageBytes.Length + 2]; 
      frame[0] = b1; 
      frame[1] = b2; 
      Array.Copy(messageBytes, 0, frame, 2, messageBytes.Length); 
     } 
     if(b2 == 126) 
     { 
      frame = new byte[messageBytes.Length + 4]; 
      frame[0] = b1; 
      frame[1] = b2; 
      byte[] lenght = BitConverter.GetBytes(messageBytes.Length); 
      frame[2] = lenght[1]; 
      frame[3] = lenght[0]; 
      Array.Copy(messageBytes, 0, frame, 4, messageBytes.Length); 
     } 

     if(b2 == 127) 
     { 
      frame = new byte[messageBytes.Length + 10]; 
      frame[0] = b1; 
      frame[1] = b2; 
      byte[] lenght = BitConverter.GetBytes((long)messageBytes.Length); 

      for(int i = 7, j = 2; i >= 0; i--, j++) 
      { 
       frame[j] = lenght[i]; 
      } 
     } 

     return frame; 
    }