WebSocket.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Core/Object.h"
  24. #include "../Container/Str.h"
  25. #include "../IO/Deserializer.h"
  26. namespace asio
  27. {
  28. class io_service;
  29. }
  30. namespace Atomic
  31. {
  32. /// WebSocket connection state
  33. enum WebSocketState
  34. {
  35. WS_CONNECTING = 0, // Establishing connection
  36. WS_OPEN, // WebSocket is open
  37. WS_CLOSING, // WebSocket is being closed (and is probably still open)
  38. WS_CLOSED, // WebSocket is closed or was disconnected
  39. WS_INVALID, // Invalid state
  40. WS_FAIL_TO_CONNECT // WebSocket attempted to open, but the server refused
  41. };
  42. struct WebSocketInternalState;
  43. /// A WebSocket connection.
  44. class WebSocket : public Object
  45. {
  46. friend class Web;
  47. OBJECT(WebSocket)
  48. public:
  49. /// Construct with parameters.
  50. WebSocket(Context* context, const String& url);
  51. /// Destruct. Release the connection object.
  52. ~WebSocket();
  53. /// Return URL used in the request.
  54. const String& GetURL() const;
  55. /// Return error. Only non-empty in the error state.
  56. String GetError() const;
  57. /// Return connection state.
  58. WebSocketState GetState() const;
  59. /// Send a message.
  60. void Send(String message);
  61. /// Disconnect the WebSocket.
  62. void Close();
  63. /// Attempt to reconnect the WebSocket.
  64. void OpenAgain();
  65. /// Return whether connection is in the open state.
  66. bool IsOpen() const { return GetState() == WS_OPEN; }
  67. private:
  68. void setup(asio::io_service *service);
  69. WebSocketInternalState* is_;
  70. };
  71. }