酷徒LOGO

easywsclient, 用于C++ 的小巧web socket客户端


  • 源代码名称:easywsclient
  • 源代码网址:http://www.github.com/dhbaird/easywsclient
  • easywsclient源代码文档
  • easywsclient源代码下载
  • Git URL:
    git://www.github.com/dhbaird/easywsclient.git
    Git Clone代码到本地:
    git clone http://www.github.com/dhbaird/easywsclient
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/dhbaird/easywsclient
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    easywsclient

    Easywsclient是一个简单而强大的WebSocket客户端,可以让你的C++代码立即连接到web堆栈,它只使用标准库,它与现代C 11 std::function和lambda兼容,支持RFC 6455版本13 WebSocket,版本13与所有主流的现代WebSocket实现兼容,包括node.js,自2011年12月以来一直都是标准。

    基本原理:这个库旨在帮助C++项目快速开始使用WebSocket,这个小库可以很容易地被抛出到现有的项目中,对于无法立即发现的复杂构建,你甚至可以通过将.cpp文件备份到项目的现有文件之一来作弊,是的,WebSocket非常棒,可以保证它集成到你的项目中!这个项目没有特别的界面需求,可以与新的C++11特性或者老的C++项目一起工作。

    easywsclient非常简单,只有一个实现文件。

    支持2014-12-06二进制帧,关闭问题#38.自动化集成测试现在通过运行make test支持,测试套件期望GoogleTest安装在/usr/src/gtest (apt-get install libgtest-dev做这个动作),测试套件使用C++14 (对于lambda捕获表达式),因此它不会在较旧的编译器上工作,请注意,easywsclient本身仍限制为c++98 =c++03,并且将继续使用旧版本的编译器进行构建。

    用法

    WebSocket类接口如下所示:

    // Factory method to create a WebSocket:static pointer from_url(std::string url);// Factory method to create a dummy WebSocket (all operations are noop):static pointer create_dummy();// Function to perform actual network send()/recv() I/O:// (note: if all you need is to recv()/dispatch() messages, then a// negative timeout can be used to block until a message arrives.// By default, when timeout is 0, poll() will not block at all.)voidpoll(int timeout = 0); // timeout in milliseconds// Receive a message, and pass it to callable(). Really, this just looks at// a buffer (filled up by poll()) and decodes any messages in the buffer.// Callable must have signature: void(const std::string & message).// Should work with C functions, C++ functors, and C++11 std::function and// lambda:template<classCallable>voiddispatch(Callable callable);// Sends a TEXT type message (gets put into a buffer for poll() to send// later):voidsend(std::string message);// Close the WebSocket (send a CLOSE message over WebSocket, then close() the// actual socket when the send buffer becomes empty):voidclose();

    组合起来,用法如下所示:

    #include"easywsclient.hpp"//#include"easywsclient.cpp" // <-- include only if you don't want compile separatelyintmain()
    {
     ...
     using easywsclient::WebSocket;
     WebSocket::pointer ws = WebSocket::from_url("ws://localhost:8126/foo");
     assert(ws);
     while (true) {
     ws->poll();
     ws->send("hello");
     ws->dispatch(handle_message);
     // ...do more stuff... }
     ...
     delete ws; // alternatively, use unique_ptr<> if you have C++11return0;
    }
    例子
    
    # Launch a test server:
    
    
    node example-server.js
    
    
    
    # Build and launch the client:
    
    
    g++ -c easywsclient.cpp -o easywsclient.o
    
    
    g++ -c example-client.cpp -o example-client.o
    
    
    g++ example-client.o easywsclient.o -o example-client
    
    
    ./example-client
    
    
    
    # ...or build and launch a C++11 client:
    
    
    g++ -std=gnu++0x -c easywsclient.cpp -o easywsclient.o
    
    
    g++ -std=gnu++0x -c example-client-cpp11.cpp -o example-client-cpp11.o
    
    
    g++ example-client-cpp11.o easywsclient.o -o example-client-cpp11
    
    
    ./example-client-cpp11
    
    
    
    # Expect the output from example-client:
    
    
    Connected to: ws://localhost:8126/foo
    
    
    >>> galaxy
    
    
    >>> world
    
    
    
    
    线程

    此库不是线程安全的,如果从多线程访问WebSocket的实例,用户必须小心使用锁,如果你需要一个快速的线程库,并且没有Boost或其他东西,我建议TinyThread





    Copyright © 2011 HelpLib All rights reserved.    知识分享协议 京ICP备17041772号-2  |  如果智培  |  酷兔英语  |  帮酷