std::string data (const char*) buf->data (), bytes_transferred); Recycle_buffer (BUF); std::string key= " Sec-websocket-key: "; Auto pos = Data.find (Key); auto Posend = Data.find (" \ r \ n ", POS); Auto value = Data.substr (pos + Key.leng Th (), Posend-(pos + key.length ())), std::string sha1src = Trim (value), sha1src + = std::string (" 258eafa5-e914-47da-95ca-c5ab0dc85b11 "); unsigned char sha1out[20];sha1 ((const unsigned char *) sha1src.c_str (), Sha1src.length (), sha1out); std::vector<unsigned char> data64;for (auto c:sha1out) Data64.push_back (c); std::o StringStream os_rsp;os_rsp<< "http/1.1 101 switching protocols\r\n" << "upgrade:websocket\r\n " << "connection:upgrade\r\n" << "sec-websocket-accept:" <<base64encode (data64) << "=\r\n " <<" \ r \ n "; std::string rsp = Os_rsp.str ();
1) data is the handshake protocol sent by the web socketclient after the connection is established.
2) Query the value of the field Sec-websocket_key. And stitching on the GUID string "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" value.
Get SHA1SRC
3) Calculate the SHA1 value after the key stitching value, get sha1out
4) Response, the most basic is to calculate the sec-websocket-accept value, through the function base64encode calculation;
Using namespace std;static const unsigned char bt[64]={' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ' , ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' z ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' + ', '/'};std::string Base64Encode ( Const std::vector<unsigned char> & Data) {std::list< std::bitset<8> > bits;for (auto c:data) { Std::bitset<8> bit (c); Bits.push_back (bit);} while (Bits.size ()% 3! = 0) Bits.push_back (bitset<8> ()); std::vector<unsigned char> Base64;while (!bits.emp Ty ()) {std::bitset<6> bit6_1,bit6_2,bit6_3,bit6_4; std::bitset<8> bit8_1 = *bits.begin (); Bits.pop_front (); std::bitset<8> bit8_2 = *bits.begin (); Bits.pop_front (); std::bitset<8> bit8_3 = *bits.begin (); Bits.pop_front (); Bit6_1.set (0, bit8_1[2]); Bit6_1.set (1, bit8_1[3]); Bit6_1.set (2, bit8_1[4]); Bit6_1.set (3, bit8_1[5]); Bit6_1.set (4, bit8_1[6]); Bit6_1.set (5, bit8_1[7]); Bit6_2.set (0, bit8_2[4]); Bit6_2.set (1, bit8_2[5]); Bit6_2.set (2, bit8_2[6]); Bit6_2.set (3, bit8_2[7]); Bit6_2.set (4, bit8_1[0]); Bit6_2.set (5, bit8_1[1]); Bit6_3.set (0, bit8_3[6]); Bit6_3.set (1, bit8_3[7]); Bit6_3.set (2, bit8_2[0]); Bit6_3.set (3, bit8_2[1]); Bit6_3.set (4, bit8_2[2]); Bit6_3.set (5, bit8_2[3]); Bit6_4.set (0, bit8_3[0]); Bit6_4.set (1, bit8_3[1]); Bit6_4.set (2, bit8_3[2]); Bit6_4.set (3, bit8_3[3]); Bit6_4.set (4, bit8_3[4]); Bit6_4.set (5, bit8_3[5]); Base64.push_back (Bt[bit6_1.to_ulong ()); Base64.push_back (Bt[bit6_2.to_ulong ()); Base64.push_back (Bt[bit6_3.to_ulong ()); Base64.push_back (Bt[bit6_4.to_ulong ());} Base64.pop_back (); string strdata (Base64.begin (), Base64.end ()); return strdata;}
5) After calculation, the RSP can be taken out.
Web Socket rfc6455 Handshake (c + +)