加入CODE CHINA

· 不限速    · 不限空间    · 不限人数    · 私仓免费

免费加入
README.md

easywsclient 20191101

Easywsclient is an easy and powerful WebSocket client to get your C++ code connected to a web stack right away. It depends only on the standard libraries. It is compatible with modern C++11 std::function and lambda, if they're available (it's not required though). RFC 6455 Version 13 WebSocket is supported. Version 13 is compatible with all major, modern WebSocket implementations, including Node.js, and has been a standard since December 2011.

Rationale: This library is intended to help a C++ project start using WebSocket rapidly. This small library can easily be thrown into an existing project. For complicated builds that you can't figure out right away, you can even cheat by piggy-backing the .cpp file into one of the project's existing files. Yes, WebSocket is that awesome enough to warrant getting it integrated into your project! This project imposes no special interface requirements, and can work happily with new C++11 features or with older C++ projects.

As an additional benefit, easywsclient is very simple, with just a single implementation file. It can serve as a cruft-free concise reference. You are most welcome to use this code as a reference for creating alternative implementations that may better suit your needs.

News

2014-12-06 Binary frames now supported. Closes issue #38. Automated integration testing is now supported by running make test. The test suite expects GoogleTest to be installed at /usr/src/gtest (apt-get install libgtest-dev does the trick). The test suite uses C++14 (for lambda capture expressions), and thus it will not work on older compilers. Note that easywsclient itself still restricted to C++98/C++03, and will continue to build with older compilers.

Usage

The WebSocket class interface looks like this:

// 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.)
void poll(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<class Callable>
void dispatch(Callable callable);

// Sends a TEXT type message (gets put into a buffer for poll() to send
// later):
void send(std::string message);

// Close the WebSocket (send a CLOSE message over WebSocket, then close() the
// actual socket when the send buffer becomes empty):
void close();

Put together, the usage looks like this:

#include "easywsclient.hpp"
//#include "easywsclient.cpp" // <-- include only if you don't want compile separately

int
main()
{
    ...
    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++11
    return 0;
}

//线程thread 维护重连连接 open a thread to maintain connect statute void run() { bool conn = FLASE; ws = WebSocket::from_url("ws://localhost:8126/foo");

//如果你需要多线程,可以在一个thread 维护该ws的连接重连机制
while (1) //判断ws是否正常连接
{
  if(ws != NULL)
  {
      ws->poll(0);//这个必须要调用,否则不能发送,发送跟接收都是异步的,都是在这个poll函数里监测处    理的
      ws->dispatch(handle_message);
      if(ws->getReadyState() == WebSocket::CLOSED)  
      {
         //ws连接断开 重连
         delete ws;
         ws = NULL;
         ws = WebSocket::from_url("ws://localhost:8126/foo");
      }
      else if(wss->getReadyState()== WebSocket::OPEN)
      {
         //ws连接ok
         //    ws->send("goodbye");
         ws->send("hello");
      }
      
  }
  else
  {
    ws = WebSocket::from_url("ws://localhost:8126/foo");
    sleep(1);
  }
  usleep(100);
}
if(ws!=NULL)
delete ws;

}

Example

# 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

Threading

This library is not thread safe. The user must take care to use locks if accessing an instance of WebSocket from multiple threads. If you need a quick threading library and don't have Boost or something else already, I recommend TinyThread++.

Future Work

(contributions appreciated!)

  • Parameterize the pointer type (especially for shared_ptr).
  • Support optional integration on top of an async (event-driven) library, especially Asio.

项目简介

🚀 Github 镜像仓库 🚀

源项目地址

https://github.com/core1011/websocket

发行版本

当前项目没有发行版本

贡献者 2

开发语言

  • C++ 87.2 %
  • JavaScript 8.2 %
  • Makefile 4.6 %