Skip to content

Commit 826e690

Browse files
committed
Fix of WebSocket close status frame
1 parent d5f4823 commit 826e690

File tree

13 files changed

+99
-24
lines changed

13 files changed

+99
-24
lines changed

examples/ws_chat_server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class ChatSession : public CppServer::WS::WSSession
4242

4343
// If the buffer starts with '!' the disconnect the current session
4444
if (message == "!")
45-
Close(1000);
45+
Close();
4646
}
4747

4848
void onError(int error, const std::string& category, const std::string& message) override

examples/wss_chat_server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class ChatSession : public CppServer::WS::WSSSession
4242

4343
// If the buffer starts with '!' the disconnect the current session
4444
if (message == "!")
45-
Close(1000);
45+
Close();
4646
}
4747

4848
void onError(int error, const std::string& category, const std::string& message) override

include/server/version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ GitHub: https://github.com/chronoxor/CppServer
2828
namespace CppServer {
2929

3030
//! Project version
31-
const char version[] = "1.0.4.0";
31+
const char version[] = "1.0.4.1";
3232

3333
} // namespace CppServer
3434

include/server/ws/ws.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,9 @@ class WebSocket
142142
/*!
143143
\param buffer - Received buffer
144144
\param size - Received buffer size
145+
\param status - WebSocket status (defualt is 1000)
145146
*/
146-
virtual void onWSClose(const void* buffer, size_t size) {}
147+
virtual void onWSClose(const void* buffer, size_t size, int status = 1000) {}
147148
//! Handle WebSocket ping notification
148149
/*!
149150
\param buffer - Received buffer

include/server/ws/ws_client.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@ class WSClient : public HTTP::HTTPClient, protected WebSocket
4040
bool Connect(const std::shared_ptr<Asio::TCPResolver>& resolver) override;
4141
bool ConnectAsync() override;
4242
bool ConnectAsync(const std::shared_ptr<Asio::TCPResolver>& resolver) override;
43-
virtual bool Close(int status) { SendClose(status, nullptr, 0); HTTPClient::Disconnect(); return true; }
44-
virtual bool CloseAsync(int status) { SendCloseAsync(status, nullptr, 0); HTTPClient::DisconnectAsync(); return true; }
43+
virtual bool Close() { return Close(0, nullptr, 0); }
44+
virtual bool Close(int status) { return Close(status, nullptr, 0); }
45+
virtual bool Close(int status, const void* buffer, size_t size) { SendClose(status, buffer, size); HTTPClient::Disconnect(); return true; }
46+
virtual bool Close(int status, std::string_view text) { SendClose(status, text); HTTPClient::Disconnect(); return true; }
47+
virtual bool CloseAsync() { return CloseAsync(0, nullptr, 0); }
48+
virtual bool CloseAsync(int status) { return CloseAsync(status, nullptr, 0); }
49+
virtual bool CloseAsync(int status, const void* buffer, size_t size) { SendCloseAsync(status, buffer, size); HTTPClient::DisconnectAsync(); return true; }
50+
virtual bool CloseAsync(int status, std::string_view text) { SendCloseAsync(status, text); HTTPClient::DisconnectAsync(); return true; }
4551

4652
// WebSocket send text methods
4753
size_t SendText(const void* buffer, size_t size) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_TEXT, true, buffer, size); return HTTPClient::Send(_ws_send_buffer.data(), _ws_send_buffer.size()); }
@@ -98,7 +104,7 @@ class WSClient : public HTTP::HTTPClient, protected WebSocket
98104
void onReceivedResponseError(const HTTP::HTTPResponse& response, const std::string& error) override;
99105

100106
//! Handle WebSocket close notification
101-
void onWSClose(const void* buffer, size_t size) override { CloseAsync(1000); }
107+
void onWSClose(const void* buffer, size_t size, int status = 1000) override { CloseAsync(); }
102108
//! Handle WebSocket ping notification
103109
void onWSPing(const void* buffer, size_t size) override { SendPongAsync(buffer, size); }
104110
//! Handle WebSocket error notification

include/server/ws/ws_server.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ class WSServer : public HTTP::HTTPServer, protected WebSocket
3838
WSServer& operator=(WSServer&&) = delete;
3939

4040
// WebSocket connection methods
41-
virtual bool CloseAll(int status);
41+
virtual bool CloseAll() { return CloseAll(0, nullptr, 0); }
42+
virtual bool CloseAll(int status) { return CloseAll(status, nullptr, 0); }
43+
virtual bool CloseAll(int status, const void* buffer, size_t size);
44+
virtual bool CloseAll(int status, std::string_view text);
4245

4346
//! Multicast data to all connected WebSocket sessions
4447
bool Multicast(const void* buffer, size_t size) override;

include/server/ws/ws_session.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ class WSSession : public HTTP::HTTPSession, protected WebSocket
3737
WSSession& operator=(WSSession&&) = delete;
3838

3939
// WebSocket connection methods
40-
virtual bool Close(int status) { SendCloseAsync(status, nullptr, 0); HTTPSession::Disconnect(); return true; }
40+
virtual bool Close() { return Close(0, nullptr, 0); }
41+
virtual bool Close(int status) { return Close(status, nullptr, 0); }
42+
virtual bool Close(int status, const void* buffer, size_t size) { SendCloseAsync(status, buffer, size); HTTPSession::Disconnect(); return true; }
43+
virtual bool Close(int status, std::string_view text) { SendCloseAsync(status, text); HTTPSession::Disconnect(); return true; }
4144

4245
// WebSocket send text methods
4346
size_t SendText(const void* buffer, size_t size) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_TEXT, false, buffer, size); return HTTPSession::Send(_ws_send_buffer.data(), _ws_send_buffer.size()); }
@@ -93,7 +96,7 @@ class WSSession : public HTTP::HTTPSession, protected WebSocket
9396
void onReceivedRequestError(const HTTP::HTTPRequest& request, const std::string& error) override;
9497

9598
//! Handle WebSocket close notification
96-
void onWSClose(const void* buffer, size_t size) override { Close(1000); }
99+
void onWSClose(const void* buffer, size_t size, int status = 1000) override { Close(); }
97100
//! Handle WebSocket ping notification
98101
void onWSPing(const void* buffer, size_t size) override { SendPongAsync(buffer, size); }
99102
//! Handle WebSocket error notification

include/server/ws/wss_client.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@ class WSSClient : public HTTP::HTTPSClient, protected WebSocket
4040
bool Connect(const std::shared_ptr<Asio::TCPResolver>& resolver) override;
4141
bool ConnectAsync() override;
4242
bool ConnectAsync(const std::shared_ptr<Asio::TCPResolver>& resolver) override;
43-
virtual bool Close(int status) { SendClose(status, nullptr, 0); HTTPSClient::Disconnect(); return true; }
44-
virtual bool CloseAsync(int status) { SendCloseAsync(status, nullptr, 0); HTTPSClient::DisconnectAsync(); return true; }
43+
virtual bool Close() { return Close(0, nullptr, 0); }
44+
virtual bool Close(int status) { return Close(status, nullptr, 0); }
45+
virtual bool Close(int status, const void* buffer, size_t size) { SendClose(status, buffer, size); HTTPSClient::Disconnect(); return true; }
46+
virtual bool Close(int status, std::string_view text) { SendClose(status, text); HTTPSClient::Disconnect(); return true; }
47+
virtual bool CloseAsync() { return CloseAsync(0, nullptr, 0); }
48+
virtual bool CloseAsync(int status) { return CloseAsync(status, nullptr, 0); }
49+
virtual bool CloseAsync(int status, const void* buffer, size_t size) { SendCloseAsync(status, buffer, size); HTTPSClient::DisconnectAsync(); return true; }
50+
virtual bool CloseAsync(int status, std::string_view text) { SendCloseAsync(status, text); HTTPSClient::DisconnectAsync(); return true; }
4551

4652
// WebSocket send text methods
4753
size_t SendText(const void* buffer, size_t size) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_TEXT, true, buffer, size); return HTTPSClient::Send(_ws_send_buffer.data(), _ws_send_buffer.size()); }
@@ -98,7 +104,7 @@ class WSSClient : public HTTP::HTTPSClient, protected WebSocket
98104
void onReceivedResponseError(const HTTP::HTTPResponse& response, const std::string& error) override;
99105

100106
//! Handle WebSocket close notification
101-
void onWSClose(const void* buffer, size_t size) override { CloseAsync(1000); }
107+
void onWSClose(const void* buffer, size_t size, int status = 1000) override { CloseAsync(); }
102108
//! Handle WebSocket ping notification
103109
void onWSPing(const void* buffer, size_t size) override { SendPongAsync(buffer, size); }
104110
//! Handle WebSocket error notification

include/server/ws/wss_server.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ class WSSServer : public HTTP::HTTPSServer, protected WebSocket
3838
WSSServer& operator=(WSSServer&&) = delete;
3939

4040
// WebSocket connection methods
41-
virtual bool CloseAll(int status);
41+
virtual bool CloseAll() { return CloseAll(0, nullptr, 0); }
42+
virtual bool CloseAll(int status) { return CloseAll(status, nullptr, 0); }
43+
virtual bool CloseAll(int status, const void* buffer, size_t size);
44+
virtual bool CloseAll(int status, std::string_view text);
4245

4346
//! Multicast data to all connected WebSocket sessions
4447
bool Multicast(const void* buffer, size_t size) override;

include/server/ws/wss_session.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ class WSSSession : public HTTP::HTTPSSession, protected WebSocket
3737
WSSSession& operator=(WSSSession&&) = delete;
3838

3939
// WebSocket connection methods
40-
virtual bool Close(int status) { SendCloseAsync(status, nullptr, 0); HTTPSSession::Disconnect(); return true; }
40+
virtual bool Close() { return Close(0, nullptr, 0); }
41+
virtual bool Close(int status) { return Close(status, nullptr, 0); }
42+
virtual bool Close(int status, const void* buffer, size_t size) { SendCloseAsync(status, buffer, size); HTTPSSession::Disconnect(); return true; }
43+
virtual bool Close(int status, std::string_view text) { SendCloseAsync(status, text); HTTPSSession::Disconnect(); return true; }
4144

4245
// WebSocket send text methods
4346
size_t SendText(const void* buffer, size_t size) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_TEXT, false, buffer, size); return HTTPSSession::Send(_ws_send_buffer.data(), _ws_send_buffer.size()); }
@@ -93,7 +96,7 @@ class WSSSession : public HTTP::HTTPSSession, protected WebSocket
9396
void onReceivedRequestError(const HTTP::HTTPRequest& request, const std::string& error) override;
9497

9598
//! Handle WebSocket close notification
96-
void onWSClose(const void* buffer, size_t size) override { Close(1000); }
99+
void onWSClose(const void* buffer, size_t size, int status = 1000) override { Close(); }
97100
//! Handle WebSocket ping notification
98101
void onWSPing(const void* buffer, size_t size) override { SendPongAsync(buffer, size); }
99102
//! Handle WebSocket error notification

0 commit comments

Comments
 (0)