websocket c++ example
2013-12-24 17:43
351 查看
//============================================================================ // Name : websocket.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <Poco/Net/WebSocket.h> #include <Poco/Net/HTTPClientSession.h> #include <Poco/Net/HTTPRequest.h> #include <Poco/Net/HTTPResponse.h> #include <Poco/Net/ServerSocket.h> #include <Poco/Net/NetException.h> #include <Poco/Exception.h> using Poco::Net::HTTPClientSession; using Poco::Net::HTTPRequest; using Poco::Net::HTTPResponse; using Poco::Net::HTTPServerRequest; using Poco::Net::HTTPServerResponse; using Poco::Net::WebSocket; using Poco::Net::WebSocketException; using Poco::Exception; using namespace std; int ws_main() { char buffer[1024]; int flags; int n; std::string payload; try { HTTPClientSession cs("echo.websocket.org", 80); HTTPRequest request(HTTPRequest::HTTP_GET, "/", "HTTP/1.1"); HTTPResponse response; std::string cmd; WebSocket * ws = new WebSocket(cs, request, response); // Causes the timeout payload = "SGClient: Hello World!"; cout << "Send: SGClient: Hello World!" << endl; ws->sendFrame(payload.data(), payload.size(), WebSocket::FRAME_TEXT); n = ws->receiveFrame(buffer, sizeof(buffer), flags); buffer = '\0'; cout << "Received: " << buffer << endl; while (cmd != "exit") { cmd = ""; cout << "Please input[exit]:"; std::cin >> cmd; ws->sendFrame(cmd.data(), cmd.size(), WebSocket::FRAME_TEXT); n = ws->receiveFrame(buffer, sizeof(buffer), flags); buffer = '\0'; if (n > 0) { std::cout << "Receive: " << buffer << std::endl; } } ws->shutdown(); } catch (Exception ex) { cout << ex.displayText() << endl; cout << ex.what() << endl; return -1; } }
相关文章推荐
- 如何配置Nginx作为WebSocket的反向代理
- Netty与TCP、HTTP、WebSocket协议简明摘要
- 用Ruby和Websocket实现实时通讯-GoEasy
- WebSocket 是什么原理?为什么可以实现持久连接?
- WebSocket详解
- websocket聊天室,简单版本,基于swoole
- 基于WebSocket的多人在线坦克大战demo
- WebSocket实战之————Workerman服务器的安装启动
- HTTP 和 WebSocket 协议(下)
- 理解websocket和socket.io
- websocket 入门及提高案例
- WebSocket详解(一):初步认识WebSocket技术
- Socket学习 - 撕开websocket神秘的外衣
- 利用html 5 websocket做个山寨版web聊天室(手写C#服务器)
- HTTP协议系列(3)---包括WebSocket简单介绍
- 用nodejs搭建websocket服务器
- WebSocket 原理介绍及服务器搭建
- websocket 实现消息推送(转)
- Spring WebSocket初探2 (Spring WebSocket入门教程)
- Python基于websocket实时通信的实现—GoEasy