The C++ framework for developing highly scalable, high performance servers on Windows platforms.

Example Servers - WebSockets Secure Echo Server

This example shows you how to build WebSockets server which uses SSL to provide a secure connection. This example uses our WebSocket Tools Library to implement the WebSocket protocol and our OpenSSL Tools Library to provide the SSL implementation. The basic structure of the server is very similar to the Basic Echo Server and Simple Text WebSocket Echo Server examples and you should go and read about those first and have a good understanding of how everything fits together. This document will only cover the differences between those examples and this one.

This example requires both the "WebSockets" and "OpenSSL" licensing options of The Server Framework and it requires libraries that only ship with these options (see here for licensing options). You can always download the latest version of this example from here; and although you will need the correct libraries to be able to build it you can look at the example code and see how it works and perhaps get ideas from it. A compiled, unicode release, build of this example is available on request if you require it for performance analysis of the framework.

Due to the pluggable nature of The Server Framework's SSL Option Pack the changes required to enable SSL on a server are minimal. The server class derives from JetByteTools::OpenSSL::CStreamSocketServerEx rather than JetByteTools::Socket::CStreamSocketServerEx and we provide an instance of JetByteTools::OpenSSL::CContext to the server's constructor. The WebSocket protocol handler allocators specify that the connections that will be created will be secure by passing the appropriate flags.

 CSocketServer::CSocketServer(
    const CContext &sslContext,
    const bool verifyPeer,
    const IFullAddress &address,
    const ListenBacklog listenBacklog,
    IIOPool &pool,
    IAllocateSequencedStreamSockets &socketAllocator,
    IAllocateBuffers &bufferAllocator,
    ILimitConnections &connectionLimiter)
    :  CStreamSocketServerEx(sslContext, verifyPeer, address, listenBacklog, *this, pool, socketAllocator, bufferAllocator, NoZeroByteRead, connectionLimiter),
       m_protocolHandlerIndex(socketAllocator.RequestUserDataSlot(_T("m_protocolHandlerIndex"))),
       m_nextBufferIndex(bufferAllocator.RequestUserDataSlot(_T("m_nextBufferIndex"))),
       m_bufferAllocator(bufferAllocator),
       m_protocolHandlerAllocator(*static_cast<JetByteTools::WebSocket::HyBi::IWebSocketServer *>(this), bufferAllocator)
 {
    m_protocolHandlerAllocator.SupportHixie76(
       *this,
       CHixie76ProtocolHandler::SecureConnection |
       CHixie76ProtocolHandler::AccumulateCompleteMessage);

    m_protocolHandlerAllocator.SupportHyBiVersions(
       CAutoDetectProtocolHandlerAllocator::HyBiProtocolAllVersions,
       *this,
       CHyBi08ProtocolHandler::SecureConnection |
       CHyBi08ProtocolHandler::AccumulateCompleteMessage);
 }

We then use the secure connection establishment callback to wire up our protocol handler.

 void CSocketServer::OnSecureConnectionEstablished(
    IStreamSocket &socket,
    const IAddress & /*address*/)
 {
    Output(_T("OnSecureConnectionEstablished"));

    CAutoDetectProtocolHandler *pHandler = m_protocolHandlerAllocator.Allocate(socket, true);

    socket.SetUserPointer(m_protocolHandlerIndex, pHandler);

    pHandler->AcceptHandshake();
 }

And that's it.

Generated on Sun Sep 12 19:06:45 2021 for The Server Framework - v7.4 by doxygen 1.5.3