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.length(),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::ostringstream 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 为连接建立以后,web socketclient发送的握手协议。

2)查询到字段 Sec-WebSocket_key的值,而且拼接上GUID字符串"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"值。

得到 sha1Src

3)对 key拼接后的值计算 sha1值,得到 sha1Out

4)应答,最基本的就是计算 Sec-WebSocket-Accept值。通过函数 base64Encode进行计算。

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.empty() ){
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) 计算后把RSP发生出去能够。

版权声明:本文博主原创文章。博客,未经同意不得转载。

Web Socket rfc6455 握 (C++)的更多相关文章

  1. web socket RFC6455 frame 打包、解包

    #ifndef __APP_WEBSOCKET_FRAME_H__ #define __APP_WEBSOCKET_FRAME_H__ #include "memory.hpp" ...

  2. Web Socket rfc6455 握手 (C++)

    std::string data((const char*)buf->data(),bytes_transferred); recycle_buffer(buf); std::string ke ...

  3. web socket (记录下来方便观看)

    Web Sockets HTML5 WebSocket 设计出来的目的就是要取代轮询和 Comet 技术,使客户端浏览器具备像 C/S 架构下桌面系统的实时通讯能力. 浏览器通过 JavaScript ...

  4. Node.js + Web Socket 打造即时聊天程序嗨聊

    前端一直是一块充满惊喜的土地,不仅是那些富有创造性的页面,还有那些惊赞的效果及不断推出的新技术.像node.js这样的后端开拓者直接将前端人员的能力扩大到了后端.瞬间就有了一统天下的感觉,来往穿梭于前 ...

  5. HTML5:web socket 和 web worker

    a:hover { cursor: pointer } 做练习遇到了一个选择题,是关于web worker的,问web worker会不会影响页面性能?补习功课之后,答案是不会影响. 查阅了相关资料学 ...

  6. 【转】轮询、长轮询、iframe长连接、web socket

    引自:http://www.cnblogs.com/AloneSword/p/3517463.html http://www.cnblogs.com/wei2yi/archive/2011/03/23 ...

  7. ASP.NET Web API上实现 Web Socket

    1. 什么是Web Socket Web Socket是Html5中引入的通信机制,它为浏览器与后台服务器之间提供了基于TCP的全双工的通信通道.用以替代以往的LongPooling等comet st ...

  8. Comet、SSE、Web Socket

    来自<javascript高级程序设计 第三版:作者Nicholas C. Zakas>的学习笔记(十一) Comet Comet是一种更加高级的Ajax技术("服务器推送&qu ...

  9. web socket 心跳包的实现方案

    web socket 心跳包的实现方案05/30/2010 现在网络环境错综复杂,socket心跳包是获得健康强壮的连接的有效解决方案,今天,我们就在web socket中实现心跳包方案,是的,尽管我 ...

随机推荐

  1. 定位vc运行时库问题 依赖问题,屡试不爽的一招

    用vc 菜单 文件| 打开|指定EXE或DLL,如有指定运行时库,则PE文件的资源中可以看到manifest 配置节 然后据此判断EXE依赖的运行时库, 再根据编译选项调整 运行时库设置

  2. 第二章排错的工具:调试器Windbg(上)

    感谢博主 http://book.51cto.com/art/200711/59731.htm <Windows用户态程序高效排错>第二章主要介绍用户态调试相关的知识和工具.本文主要讲了排 ...

  3. 简单的Ajax应用实例

    从网页前端输入提示范围内的字符,然后显示从后台返回的结果 <html> <head> <meta http-equiv="content-type" ...

  4. CodeForces 13E. Holes 分块处理

    正解是动态树,太难了,仅仅好分块处理水之.看了看status大概慢了一倍之多..     分块算法大体就是在找一个折衷点,使得查询和改动的时间复杂度都不算太高,均为o(sqrt(n)),所以总的时间复 ...

  5. A Game of Thrones(19) - Jon

    The courtyard rang to the song of swords. Under black wool, boiled leather, and mail, sweat trickled ...

  6. 温故知新-------jQuery层次选择器

    <html xmlns="http://www.w3.org/1999/xhtml">  <head>     <title></titl ...

  7. UDP vs. TCP

    UDP vs. TCP 原文:UDP vs. TCP,作者是Glenn Fiedler,专注于游戏网络编程相关工作多年. 说在最前面的话 翻译这篇文章的初衷:我在工作中根本接触不到网络游戏编程,但是我 ...

  8. Unigine 基础入门

    1. 首先要搭建好开发环境: 1)Visual Stodio 已经安装了. 2). Microsoft Windows SDK 7.1 (for Windows 7): https://www.mic ...

  9. chmod u+s(转)

    参看了 http://hi.baidu.com/hehongrong/item/b64a6d6b094cf634ac3e8382 里面说 -s :在文件执行时把进程的属主或组ID置为该文件的文件属主. ...

  10. Codeforces 461B - Appleman and Tree 树状DP

    一棵树上有K个黑色节点,剩余节点都为白色,将其划分成K个子树,使得每棵树上都仅仅有1个黑色节点,共同拥有多少种划分方案. 个人感觉这题比較难. 如果dp(i,0..1)代表的是以i为根节点的子树种有0 ...