1 // Copyright 2024 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "net/test/embedded_test_server/websocket_handler.h" 6 7 #include "base/memory/scoped_refptr.h" 8 #include "net/test/embedded_test_server/websocket_connection.h" 9 10 namespace net::test_server { 11 WebSocketHandler(scoped_refptr<WebSocketConnection> connection)12WebSocketHandler::WebSocketHandler( 13 scoped_refptr<WebSocketConnection> connection) 14 : connection_(std::move(connection)) {} 15 16 WebSocketHandler::~WebSocketHandler() = default; 17 18 // Default implementation of OnPing that responds with a PONG message. OnPing(base::span<const uint8_t> payload)19void WebSocketHandler::OnPing(base::span<const uint8_t> payload) { 20 if (connection()) { 21 connection()->SendPong(payload); 22 } 23 } 24 25 // Default implementation of OnPong that does nothing. OnPong(base::span<const uint8_t> payload)26void WebSocketHandler::OnPong(base::span<const uint8_t> payload) { 27 // Default implementation does nothing. 28 DVLOG(3) << "Received PONG message."; 29 } 30 31 // Default implementation of OnClosingHandshake. OnClosingHandshake(std::optional<uint16_t> code,std::string_view message)32void WebSocketHandler::OnClosingHandshake(std::optional<uint16_t> code, 33 std::string_view message) { 34 DVLOG(3) << "Closing handshake received with code: " 35 << (code.has_value() ? base::NumberToString(code.value()) : "none") 36 << ", message: " << message; 37 38 connection()->RespondToCloseFrame(code, message); 39 } 40 41 } // namespace net::test_server 42