UnrealVoxta 0.1.1
 
Loading...
Searching...
No Matches
Connection.h
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2020-2022 Frozen Storm Interactive, Yoann Potinet
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#pragma once
26
27#include "CoreMinimal.h"
28#include "IWebSocket.h"
29#include "Interfaces/IHttpRequest.h"
30
36class SIGNALR_API FConnection : public TSharedFromThis<FConnection>
37{
38public:
45 FConnection(const FString& InHost, const TMap<FString, FString>& InHeaders);
46
51 virtual ~FConnection();
52
53 FConnection(const FConnection&) = delete;
57
61 void Connect();
62
68 bool IsConnected() const;
69
75 void Send(const FString& Data);
76
83 void Close(int32 Code = 1000, const FString& Reason = FString());
84
85 DECLARE_EVENT(FConnection, FConnectionFailedEvent);
86
92 FConnectionFailedEvent& OnConnectionFailed();
93
99 IWebSocket::FWebSocketConnectedEvent& OnConnected();
100
106 IWebSocket::FWebSocketConnectionErrorEvent& OnConnectionError();
107
113 IWebSocket::FWebSocketClosedEvent& OnClosed();
114
120 IWebSocket::FWebSocketMessageEvent& OnMessage();
121
122private:
123 void Negotiate();
124 void OnNegotiateResponse(FHttpRequestPtr InRequest, FHttpResponsePtr InResponse, bool bConnectedSuccessfully);
125 void StartWebSocket();
126
127 TSharedPtr<IWebSocket> Connection;
128 FString Host;
129 TMap<FString, FString> Headers;
130
131 FConnectionFailedEvent OnConnectionFailedEvent;
132 IWebSocket::FWebSocketConnectedEvent OnConnectedEvent;
133 IWebSocket::FWebSocketConnectionErrorEvent OnConnectionErrorEvent;
134 IWebSocket::FWebSocketClosedEvent OnClosedEvent;
135 IWebSocket::FWebSocketMessageEvent OnMessageEvent;
136
137 FString ConnectionToken;
138 FString ConnectionId;
139
140 static FString ConvertToWebsocketUrl(const FString& Url);
141};
@ Close
Definition MessageType.h:41
void Send(const FString &Data)
Sends data over the connection.
Definition Connection.cpp:60
IWebSocket::FWebSocketMessageEvent & OnMessage()
Gets the event that is triggered when a message is received.
Definition Connection.cpp:105
FConnection & operator=(const FConnection &)=delete
IWebSocket::FWebSocketClosedEvent & OnClosed()
Gets the event that is triggered when the connection is closed.
Definition Connection.cpp:100
DECLARE_EVENT(FConnection, FConnectionFailedEvent)
FConnection(const FString &InHost, const TMap< FString, FString > &InHeaders)
Creates a new connection to a SignalR server.
Definition Connection.cpp:36
FConnection(const FConnection &)=delete
bool IsConnected() const
Checks if the connection is currently established.
Definition Connection.cpp:55
FConnectionFailedEvent & OnConnectionFailed()
Gets the event that is triggered when the connection fails to establish.
Definition Connection.cpp:85
IWebSocket::FWebSocketConnectionErrorEvent & OnConnectionError()
Gets the event that is triggered when a connection error occurs.
Definition Connection.cpp:95
void Connect()
Initiates the connection process by starting the negotiation with the server.
Definition Connection.cpp:50
IWebSocket::FWebSocketConnectedEvent & OnConnected()
Gets the event that is triggered when the connection is established.
Definition Connection.cpp:90
FConnection(FConnection &&)=delete
FConnection & operator=(FConnection &&)=delete