본문 바로가기

Unreal

UE4 이벤트 쉽게 사용하기

헤더파일에서 선언하기

// 블루프린트 네이티브 이벤트 처리방식 (파라미터X)
UFUNCTION(BlueprintNativeEvent)
void OnConnected();
virtual void OnConnected_Implementation();

// 블루프린트 네이티브 이벤트 처리방식 (파라미터O)
UFUNCTION(BlueprintNativeEvent)
void OnReceived(const FString& param);
virtual FString OnReceived_Implementation(FString param);

 

cpp파일에서 사용하기

void AMyWebSocket::ConnectServer(FString serverURL, FString serverProtocol)
{
	
	//FWebSocketsModule& Module = FModuleManager::LoadModuleChecked<FWebSocketsModule>(TEXT("WebSockets"));

	//const FString ServerURL = TEXT("ws://127.0.0.1:3000/"); // Your server URL. You can use ws, wss or wss+insecure.
	//const FString ServerProtocol = TEXT("ws");              // The WebServer protocol you want to use.

	const FString ServerURL = serverURL;
	const FString ServerProtocol = serverProtocol;

	Socket = FWebSocketsModule::Get().CreateWebSocket(ServerURL, ServerProtocol);

	// We bind all available events
	Socket->OnConnected().AddLambda([this]() {
		UE_LOG(LogTemp, Log, TEXT("Connected to websocket server."));
		Socket->Send("{\"event\": \"test\", \"data\": \"test message data\"}");
        ///////////////////////////////////////////////////////////////////////
		OnConnected(); // 이벤트 발생 지점
        ///////////////////////////////////////////////////////////////////////
	});

	Socket->OnConnectionError().AddLambda([](const FString & Error) -> void {
		// This code will run if the connection failed. Check Error to see what happened.
	});

	Socket->OnClosed().AddLambda([](int32 StatusCode, const FString& Reason, bool bWasClean) -> void {
		// This code will run when the connection to the server has been terminated.
		// Because of an error or a call to Socket->Close().
	});

	Socket->OnMessage().AddLambda([this](const FString & Message) -> void {
		// This code will run when we receive a string message from the server.
		UE_LOG(LogTemp, Log, TEXT("Received message from websocket server: \"%s\"."), *Message);
        ///////////////////////////////////////////////////////////////////////
		OnReceived(Message); // 이벤트 발생 지점
        ///////////////////////////////////////////////////////////////////////
		OnMsgReceived.Broadcast(Message);
	});

	Socket->OnRawMessage().AddLambda([](const void* Data, SIZE_T Size, SIZE_T BytesRemaining) -> void {
		// This code will run when we receive a raw (binary) message from the server.
	});

	Socket->OnMessageSent().AddLambda([](const FString& MessageString) -> void {
		// This code is called after we sent a message to the server.
	});

	// And we finally connect to the server. 
	Socket->Connect();
}

 

이벤트 함수 구현부분

void AMyWebSocket::OnConnected_Implementation()
{

}

FString AMyWebSocket::OnReceived_Implementation(FString param)
{
	return param;
}

 

위와 같이 구현하면 블루프린트에서 이벤트를 구현 할 수 있다.

 

파라미터를 여러개 주고 싶다면 struct를 사용하면 된다.

USTRUCT(BlueprintType)
struct SOCKETIO_TEST_API FMyStruct
{
	GENERATED_BODY()

public:
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MySettings")
		FString DestinationIP = "hello";
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MySettings")
        int value2 = 1;
};

UCLASS()
class SOCKETIO_TEST_API AMyWebSocket : public AActor
{
	GENERATED_BODY()
    
    UFUNCTION(BlueprintNativeEvent)
		void OnTest(const FMyStruct& param);
		virtual FMyStruct OnTest_Implementation(FMyStruct param);
};

스트럭트를 클래스보다 위에 선언해주어야 에러가 나지 않았다.