websoecket created in AsyncThreadTask.
void AsyncThreadTask::DoWork() {
while(1)
{
if (!IsInGameThread() && !_USocket)
{
//
_USocket = UUSocketManager::CreateWebSocket(_UFun->WorldContextObject->GetWorld());
if (_USocket && _UFun)
{
// on connected
_USocket->OnConnected.AddDynamic(_UFun, &UThreadFun::Connected);
// on closed
_USocket->OnClosed.AddDynamic(_UFun, &UThreadFun::Closed);
// on connection error
_USocket->OnConnectionError.AddDynamic(_UFun, &UThreadFun::ConnectionError);
// on text message
_USocket->OnTextMessage.AddDynamic(_UFun, &UThreadFun::TextMessage);
// on bytes message
_USocket->OnByteMessage.AddDynamic(_UFun, &UThreadFun::ByteMessage);
// on retry
_USocket->OnConnectionRetry.AddDynamic(_UFun, &UThreadFun::Retry);
// open connection
_USocket->Open(_UFun->SocketOptions);
}
//
AsyncTask(ENamedThreads::GameThread, [=]()
{
});
}
}
}
UCLASS(Blueprintable, BlueprintType)
class FRAMEVIDEOWEBTHREADSOCKET_API UThreadFun : public UObject
{
GENERATED_BODY()
public:
UFUNCTION()
void Failed(ESocketManagerError Reason = ESocketManagerError::None, FString Error = "");
UFUNCTION()
void Connected();
UFUNCTION()
void Closed(const int32& Code, const FString& Reason, bool ClosedByPeer);
UFUNCTION()
void ConnectionError(const FString& Reason);
UFUNCTION()
void TextMessage(const FString& Message);
UFUNCTION()
void ByteMessage(const TArray<uint8>& Message);
UFUNCTION()
void Retry(const int32& RetryCount);
protected:
FVideoFrame parseVideoFrame(const FString& msg);
public:
FSocketManagerOptions SocketOptions;
const UObject* WorldContextObject = nullptr;
protected:
UJsonDataParse DataParse;
};
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnConnectedDelegate);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FOnClosedDelegate, const int32&, Code, const FString&, Reason, bool, ClosedByPeer);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnConnectionErrorDelegate, const FString&, ErrorReason);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnTextMessageDelegate, const FString&, Message);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnMessageSentDelegate, const FString&, Message);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnByteMessageDelegate, const TArray<uint8>&, Message);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnRetryDelegate, const int32&, RetryCount);
/**
*
*/
UCLASS()
class FRAMEVIDEOWEBTHREADSOCKET_API UUSocketManager : public UObject
{
GENERATED_BODY()
public:
UPROPERTY(BlueprintAssignable, Category = "SocketManagerEvent")
FOnConnectedDelegate OnConnected;
UPROPERTY(BlueprintAssignable, Category = "SocketManager|Event")
FOnClosedDelegate OnClosed;
UPROPERTY(BlueprintAssignable, Category = "SocketManager|Event")
FOnConnectionErrorDelegate OnConnectionError;
UPROPERTY(BlueprintAssignable, Category = "SocketManager|Event")
FOnTextMessageDelegate OnTextMessage;
UPROPERTY(BlueprintAssignable, Category = "SocketManager|Event")
FOnByteMessageDelegate OnByteMessage;
UPROPERTY(BlueprintAssignable, Category = "SocketManager|Event")
FOnMessageSentDelegate OnMessageSent;
UPROPERTY(BlueprintAssignable, Category = "SocketManager|Event")
FOnRetryDelegate OnConnectionRetry;
private:
FSocketManagerOptions Options;
TSharedPtr<IWebSocket> Socket;
TArray<uint8> Buffer;
FTimerHandle ReconnectHandle;
int32 ReconnectAmount = 0;
UWorld* WorldContextObject = nullptr;
public:
~UUSocketManager();
//UFUNCTION(BlueprintPure, Category = "SocketManager", meta = (WorldContext = "WorldContext"))
// static UUSocketManager* CreateWebSocket(const UObject* WorldContext);
UFUNCTION(BlueprintPure, Category = "SocketManager")
static UUSocketManager* CreateWebSocket(UWorld* WorldContextObject);
UFUNCTION(BlueprintPure, Category = "SocketManager")
bool IsConnected();
UFUNCTION(BlueprintCallable, Category = "SocketManager")
bool Open(const FSocketManagerOptions& Ops);
UFUNCTION(BlueprintCallable, Category = "SocketManager")
bool Close(int32 Code = 1000, const FString& Reason = "");
UFUNCTION(BlueprintCallable, Category = "SocketManager")
bool SendText(const FString& Data);
UFUNCTION(BlueprintCallable, Category = "SocketManager")
bool SendBytes(const TArray<uint8>& Data, bool IsBinary = false);
};