Skip to content
muhler edited this page Nov 23, 2011 · 1 revision

Include websocketpp.hpp

Create a websocketpp::server_ptr initialized to a new websocketpp::server object.

The server constructor will need three things.

  1. A boost::asio::io_service object to use to manage its async operations
  2. A boost::asio::ip::tcp::endpoint to listen to for new connections
  3. An object that implements the websocketpp::connection_handler interface to provide callback functions (See Handler API)

After construction the server object will be in the initialization state. At this time you can set up server configuration options either via calling individual set option commands or by loading them in a batch from a configuration file.

The only required option is that at least one host value must be set. Incoming websocket connections must specify a host value that they wish to connect to and if the server object does not have that host value in it’s list of canonical hosts it will reject the connection.

Once the server has been configured the way you want, call the start_accept() method. This will add the first async call to your io_service. If your io_service was already running, the server will start accepting connections immediately. If not you will need to call io_service.run() to start it.

Once the server has started it will accept new connections. A new session object will be created for each connection accepted. The session will perform the websocket handshake and if it is successful begin reading frames. The session will continue reading frames until an error occurs or a connection close frame is seen. The session will notify the handler that it was initialized with (see Handler API) as necessary. The Session API defines how a handler (or other part of the end application) can interact with the session (to get information about the session, send messages back to the client, etc).