QT Write WEBSOCKETPP service side

Source: Internet
Author: User

1, download WEBSOCKETPP, address is HTTPS://GITHUB.COM/ZAPHOYD/WEBSOCKETPP, version is 0.7.

2, download boost, the address is https://www.boost.org/, the version is 1.6.3.

3, Description: WEBSOCKETPP does not have to need boost, if C + + compiled to C11 above, it can be used.

4, QT Create a console project (such as: Websocketserver), the downloaded websocket_master inside the WebSocket folder into the project directory, Add an include (WEBSOCKET/WEBSOCKET.PRI) in the Websocketserver.pro file,

5, Create Websocket_server class, header file Websocket_server.h content as follows:

#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <websocketpp/impl/connection_impl.hpp>
#include <websocketpp/config/core.hpp>
#include <websocketpp/connection.hpp>
#include <websocketpp/endpoint.hpp>
#include <websocketpp/close.hpp>
#include <websocketpp/uri.hpp>
#include <map>
#include <string>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <QDateTime>
Using websocketpp::lib::p laceholders::_1;
Using websocketpp::lib::p laceholders::_2;
Using Websocketpp::lib::bind;
Using Websocketpp::endpoint;
Using Websocketpp::connection;
typedef websocketpp::server<websocketpp::config::asio> Server;
typedef SERVER::MESSAGE_PTR MESSAGE_PTR;
/////////////////////////////////////////////////////////////////////
Class Connection_metadata
{
Public
typedef websocketpp::lib::shared_ptr<connection_metadata> PTR;
Connection_metadata (WEBSOCKETPP::CONNECTION_HDL hdl,std::string strremote):
M_HDL (HDL), Strremote (Strremote)
{}
    void SetDateTime ()
{
DT = Qdatetime::currentdatetime ();
}
    Qdatetime GetDateTime () const
{
return DT;
}
    void Sethdl (WEBSOCKETPP::CONNECTION_HDL HDL)
{
M_HDL = HDL;
}
    WEBSOCKETPP::CONNECTION_HDL GETHDL ()
{
return M_HDL;
}
    void Update_time ()
{
DT = Qdatetime::currentdatetime ();
}
    void Setremote (std::string strremote)
{
This->strremote = Strremote;
}

std::string getremote () const
{
return strremote;
}
Private
Qdatetime DT;
Std::string strremote;
WEBSOCKETPP::CONNECTION_HDL M_HDL;
};
/////////////////////////////////////////////////////////////////////
Class Websocket_server
{
Public
Websocket_server ();
~websocket_server ();

static void On_open (server* s,websocket_server* pwebsocket,websocketpp::connection_hdl HDL);
static void On_close (server* s,websocket_server* pwebsocket,websocketpp::connection_hdl HDL);
static void On_message (server* s,websocket_server* pwebsocket,websocketpp::connection_hdl hdl,message_ptr msg);
    void start (int port);
Private
typedef std::map<std::string,connection_metadata::p tr> con_list;

Server echo_server;
Con_list m_connection_list;
    Websocketpp::lib::shared_ptr<websocketpp::lib::thread> M_thread;
};

6, Websocket_server.cpp content as follows:

Websocket_server::websocket_server ()
{
M_connection_list.clear ();
}
Websocket_server::~websocket_server ()
{
for (Con_list::iterator iter = M_connection_list.begin (); ITER! = M_connection_list.end ();)
{
Echo_server.close (ITER->SECOND->GETHDL (), Websocketpp::close::status::going_away, "");
M_connection_list.erase (iter++);
}
    Echo_server.stop_perpetual ();
M_thread->join ();
}
Connection
void Websocket_server::on_open (server *s,websocket_server* pwebsocket,websocketpp::connection_hdl HDL)
{
Server::connection_ptr con = S->GET_CON_FROM_HDL (HDL);
Const std::string strremote = Con->get_remote_endpoint ();
Con_list::iterator iter = Pwebsocket->m_connection_list.find (strremote);
    if (iter = = Pwebsocket->m_connection_list.end ())
{
connection_metadata* pmetadata = new Connection_metadata (hdl,strremote);
Pwebsocket->m_connection_list.insert (Make_pair (strremote,pmetadata));
}
Else
{
ITER->SECOND->SETHDL (HDL);
}
}
Disconnect
void Websocket_server::on_close (server* s,websocket_server* pwebsocket,websocketpp::connection_hdl HDL)
{
Server::connection_ptr con = S->GET_CON_FROM_HDL (HDL);
Const std::string strremote = Con->get_remote_endpoint ();
Con_list::iterator iter = Pwebsocket->m_connection_list.find (strremote);
    if (iter! = Pwebsocket->m_connection_list.end ())
{
Pwebsocket->m_connection_list.erase (ITER);
}
}
Communication
void Websocket_server::on_message (server* s,websocket_server* pwebsocket,websocketpp::connection_hdl HDL, Message_ PTR msg)
{
if (msg->get_opcode () = = Websocketpp::frame::opcode::text)
{
Server::connection_ptr con = S->GET_CON_FROM_HDL (HDL);
Const std::string strremote = Con->get_remote_endpoint ();
Std::cout << strremote <<std::endl;
}
    Std::cout << "On_message called with HDL:" << hdl.lock (). Get () << "and message:" << msg->get _payload () << Std::endl;

if (msg->get_payload () = = "Stop-listening")
{
S->stop_listening ();
Return
}

Try
{
S->send (HDL, Msg->get_payload (), Msg->get_opcode ());//Data received from the original path returned
}
    catch (const websocketpp::lib::error_code& e)
{
Std::cout << "Echo failed because:" << e << "(" << e.message () << ")" << Std::endl;
}
}
void Websocket_server::start (int port)
{
Try
{
Set Logging Settings
Echo_server.set_access_channels (Websocketpp::log::alevel::all);
Echo_server.clear_access_channels (Websocketpp::log::alevel::frame_payload);
        Initialize Asio
Echo_server.init_asio ();
Echo_server.start_perpetual ();
        Echo_server.set_open_handler (Bind (&on_open,&echo_server,this,::_1));
Echo_server.set_close_handler (Bind (&on_close,&echo_server,this,::_1));
Echo_server.set_message_handler (Bind (&on_message,&echo_server,this,::_1,::_2));
        Listen on Port 9002
Echo_server.listen (port);
        Start the server accept loop
Echo_server.start_accept ();
        Start the ASIO Io_service run loop
Echo_server.run ();
M_thread = websocketpp::lib::make_shared<websocketpp::lib::thread> (&server::run,&echo_server);// Run the Echo_server run with the thread m_thread run function to avoid the application card in the Echo_server run
}
catch (websocketpp::exception const & E)
{
Std::cout << e.what () << Std::endl;
}
catch (...)
{
Std::cout << "Other exception" << Std::endl;
}
}

QT Write WEBSOCKETPP service side

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.