WebSocketController

As the name implies, WebSocketController is used to process websocket logic. Websocket is a persistent HTTP-based connection scheme. At the beginning of the websocket, there is an HTTP format request and response exchange. After the websocket connection is established, all messages are transmitted on the websocket. The message is wrapped in a fixed format. There is no limit to the message content and the order in which messages are transmitted.

The source file of the WebSocketController can be generated by the drogon_ctl tool. The command format is as follows:

  1. drogon_ctl create controller -w <[namespace::]class_name>

Suppose we want to implement a simple echo function through websocket, that is, the server simply sends back the message sent by the client. We can create the implementation class EchoWebsock of WebSocketController through drogon_ctl, as follows:

  1. drogon_ctl create controller -w EchoWebsock

The command will generate two files of EchoWebsock.h and EchoWebsock.cc,as follows:

  1. //EchoWebsock.h
  2. #pragma once
  3. #include <drogon/WebSocketController.h>
  4. using namespace drogon;
  5. class EchoWebsock:public drogon::WebSocketController<EchoWebsock>
  6. {
  7. public:
  8. void handleNewMessage(const WebSocketConnectionPtr&,
  9. std::string &&,
  10. const WebSocketMessageType &) override;
  11. void handleNewConnection(const HttpRequestPtr &,
  12. const WebSocketConnectionPtr&) override;
  13. void handleConnectionClosed(const WebSocketConnectionPtr&) override;
  14. WS_PATH_LIST_BEGIN
  15. //list path definitions here;
  16. WS_PATH_LIST_END
  17. };
  1. //EchoWebsock.cc
  2. #include "EchoWebsock.h"
  3. void EchoWebsock::handleNewMessage(const WebSocketConnectionPtr &wsConnPtr,std::string &&message)
  4. {
  5. //write your application logic here
  6. }
  7. void EchoWebsock::handleNewConnection(const HttpRequestPtr &req,const WebSocketConnectionPtr &wsConnPtr)
  8. {
  9. //write your application logic here
  10. }
  11. void EchoWebsock::handleConnectionClosed(const WebSocketConnectionPtr &wsConnPtr)
  12. {
  13. //write your application logic here
  14. }

After edited:

  1. //EchoWebsock.h
  2. #pragma once
  3. #include <drogon/WebSocketController.h>
  4. using namespace drogon;
  5. class EchoWebsock:public drogon::WebSocketController<EchoWebsock>
  6. {
  7. public:
  8. virtual void handleNewMessage(const WebSocketConnectionPtr&,
  9. std::string &&,
  10. const WebSocketMessageType &)override;
  11. virtual void handleNewConnection(const HttpRequestPtr &,
  12. const WebSocketConnectionPtr&)override;
  13. virtual void handleConnectionClosed(const WebSocketConnectionPtr&)override;
  14. WS_PATH_LIST_BEGIN
  15. //list path definitions here;
  16. WS_PATH_ADD("/echo");
  17. WS_PATH_LIST_END
  18. };
  1. //EchoWebsock.cc
  2. #include "EchoWebsock.h"
  3. void EchoWebsock::handleNewMessage(const WebSocketConnectionPtr &wsConnPtr,std::string &&message)
  4. {
  5. //write your application logic here
  6. wsConnPtr->send(message);
  7. }
  8. void EchoWebsock::handleNewConnection(const HttpRequestPtr &req,const WebSocketConnectionPtr &wsConnPtr)
  9. {
  10. //write your application logic here
  11. }
  12. void EchoWebsock::handleConnectionClosed(const WebSocketConnectionPtr &wsConnPtr)
  13. {
  14. //write your application logic here
  15. }

First, in this example, the controller is registered to the /echo path via the WS_PATH_ADD macro. The usage of the WS_PATH_ADD macro is similar to the macros of other controllers introduced earlier. One can also register the path with several Filters. Since websocket is handled separately in the framework, it can be repeated with the paths of the first two controllers(HttpSimpleController and HttpApiController) without affecting each other.

Secondly, in the implementation of the three virtual functions in this example, only the handleNewMessage has the substance, but simply sends the received message back to the client through the send interface.Compile this controller into the framework, you can see the effect, please test it yourself.

Note: Like the usual HTTP protocol, http websocket can be sniffed. If security is required, encryption should be provided by HTTPS. Of course, it is also possible for users to complete encryption and decryption on the server and client side, but HTTPS is more convenient. The underlying layer is handled by drogon, and users only need to care about business logic.

The user-defined websocket controller class inherits from the drogon::WebSocketController class template. The template parameter is a subclass type. The user needs to implement the following three virtual functions to process the establishment, shutdown, and messages of the websocket:

  1. virtual void handleNewConnection(const HttpRequestPtr &req,const WebSocketConnectionPtr &wsConn);
  2. virtual void handleNewMessage(const WebSocketConnectionPtr &wsConn,std::string &&message,
  3. const WebSocketMessageType &);
  4. virtual void handleConnectionClosed(const WebSocketConnectionPtr &wsConn);

Easy to know:

  • handleNewConnection is called after the websocket is established. req is the setup request sent by the client. At this time, the framework has returned the response. What users can do is to get some additional information through req, such as token. wsConn is a smart pointer to this websocket object, and the commonly used interface will be discussed later.
  • handleNewMessage is called after the websocket receives the new message. The message is stored in the message variable. Note that the message is the message payload. The framework has finished the decapsulation and decoding of the message. The user can directly process the message itself.
  • handleConnectionClosed is called after the websocket connection is closed, and the user can do some finishing work.

WebSocketConnection

The common interfaces of the WebSocketConnection object are as follows:

  1. //Send a websocket message, the encoding and encapsulation
  2. //of the message are the responsibility of the framework
  3. void send(const char *msg,uint64_t len);
  4. void send(const std::string &msg);
  5. //Local and remote addresses of the websocket
  6. const trantor::InetAddress &localAddr() const;
  7. const trantor::InetAddress &peerAddr() const;
  8. //The connection state of the weosocket
  9. bool connected() const;
  10. bool disconnected() const;
  11. //close websocket
  12. void shutdown();//close write
  13. void forceClose();//close
  14. //set up and get the context of the websocket, and store some business data from users.
  15. //the any type means that you can store any type of object.
  16. void setContext(const any &context);
  17. const any &getContext() const;
  18. any *getMutableContext();

05 Filter