Deployed 847fc14 with MkDocs version: 1.1.2

This commit is contained in:
2020-07-25 18:43:04 +00:00
parent 9e0825127d
commit b47cb43419
5 changed files with 44 additions and 29 deletions

View File

@ -331,6 +331,7 @@ uint32_t m = webSocket.getMaxWaitBetweenReconnectionRetries();
<h2 id="websocket-server-api">WebSocket server API</h2>
<h3 id="legacy-api">Legacy api</h3>
<p>This api was actually changed to take a weak_ptr<WebSocket> as the first argument to setOnConnectionCallback ; previously it would take a shared_ptr<WebSocket> which was creating cycles and then memory leaks problems.</p>
<pre><code class="cpp">#include &lt;ixwebsocket/IXWebSocketServer.h&gt;
...
@ -340,41 +341,49 @@ uint32_t m = webSocket.getMaxWaitBetweenReconnectionRetries();
ix::WebSocketServer server(port);
server.setOnConnectionCallback(
[&amp;server](std::shared_ptr&lt;WebSocket&gt; webSocket,
[&amp;server](std::weak_ptr&lt;WebSocket&gt; webSocket,
std::shared_ptr&lt;ConnectionState&gt; connectionState,
std::unique_ptr&lt;ConnectionInfo&gt; connectionInfo)
{
std::cout &lt;&lt; &quot;Remote ip: &quot; &lt;&lt; connectionInfo-&gt;remoteIp &lt;&lt; std::endl;
webSocket-&gt;setOnMessageCallback(
[webSocket, connectionState, &amp;server](const ix::WebSocketMessagePtr msg)
{
if (msg-&gt;type == ix::WebSocketMessageType::Open)
auto ws = webSocket.lock();
if (ws)
{
ws-&gt;setOnMessageCallback(
[webSocket, connectionState, &amp;server](const ix::WebSocketMessagePtr msg)
{
std::cout &lt;&lt; &quot;New connection&quot; &lt;&lt; std::endl;
// A connection state object is available, and has a default id
// You can subclass ConnectionState and pass an alternate factory
// to override it. It is useful if you want to store custom
// attributes per connection (authenticated bool flag, attributes, etc...)
std::cout &lt;&lt; &quot;id: &quot; &lt;&lt; connectionState-&gt;getId() &lt;&lt; std::endl;
// The uri the client did connect to.
std::cout &lt;&lt; &quot;Uri: &quot; &lt;&lt; msg-&gt;openInfo.uri &lt;&lt; std::endl;
std::cout &lt;&lt; &quot;Headers:&quot; &lt;&lt; std::endl;
for (auto it : msg-&gt;openInfo.headers)
if (msg-&gt;type == ix::WebSocketMessageType::Open)
{
std::cout &lt;&lt; it.first &lt;&lt; &quot;: &quot; &lt;&lt; it.second &lt;&lt; std::endl;
std::cout &lt;&lt; &quot;New connection&quot; &lt;&lt; std::endl;
// A connection state object is available, and has a default id
// You can subclass ConnectionState and pass an alternate factory
// to override it. It is useful if you want to store custom
// attributes per connection (authenticated bool flag, attributes, etc...)
std::cout &lt;&lt; &quot;id: &quot; &lt;&lt; connectionState-&gt;getId() &lt;&lt; std::endl;
// The uri the client did connect to.
std::cout &lt;&lt; &quot;Uri: &quot; &lt;&lt; msg-&gt;openInfo.uri &lt;&lt; std::endl;
std::cout &lt;&lt; &quot;Headers:&quot; &lt;&lt; std::endl;
for (auto it : msg-&gt;openInfo.headers)
{
std::cout &lt;&lt; it.first &lt;&lt; &quot;: &quot; &lt;&lt; it.second &lt;&lt; std::endl;
}
}
else if (msg-&gt;type == ix::WebSocketMessageType::Message)
{
// For an echo server, we just send back to the client whatever was received by the server
// All connected clients are available in an std::set. See the broadcast cpp example.
// Second parameter tells whether we are sending the message in binary or text mode.
// Here we send it in the same mode as it was received.
auto ws = webSocket.lock();
if (ws)
{
ws-&gt;send(msg-&gt;str, msg-&gt;binary);
}
}
}
else if (msg-&gt;type == ix::WebSocketMessageType::Message)
{
// For an echo server, we just send back to the client whatever was received by the server
// All connected clients are available in an std::set. See the broadcast cpp example.
// Second parameter tells whether we are sending the message in binary or text mode.
// Here we send it in the same mode as it was received.
webSocket-&gt;send(msg-&gt;str, msg-&gt;binary);
}
}
);