-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebsocketSampleSubsystem.cpp
57 lines (43 loc) · 1.83 KB
/
WebsocketSampleSubsystem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Fill out your copyright notice in the Description page of Project Settings.
#include "WebsocketSampleSubsystem.h"
#include "WebSocketsModule.h"
#include "IWebSocket.h"
DEFINE_LOG_CATEGORY(LogMyWebSocket);
void UWebsocketSampleSubsystem::Initialize(FSubsystemCollectionBase& Collection)
{
const FString ServerURI = TEXT("ws://localhost:8080/ws");
const FString ServerProtocol = TEXT("ws");
Socket = FWebSocketsModule::Get().CreateWebSocket(ServerURI, ServerProtocol);
Socket->OnConnected().AddUObject(this, &UWebsocketSampleSubsystem::OnSocketConnected);
Socket->OnConnectionError().AddUObject(this, &UWebsocketSampleSubsystem::OnSocketConnectioinError);
Socket->OnClosed().AddUObject(this, &UWebsocketSampleSubsystem::OnSocketClosed);
Socket->OnMessage().AddUObject(this, &UWebsocketSampleSubsystem::OnSocketMessageReceived);
Socket->Connect();
}
void UWebsocketSampleSubsystem::Deinitialize()
{
Socket->Close();
Socket = nullptr;
}
FString UWebsocketSampleSubsystem::CurrentMessage() const
{
return currentMsg;
}
void UWebsocketSampleSubsystem::OnSocketConnected() const
{
UE_LOG(LogMyWebSocket, Log, TEXT("OnSocketConnected"));
}
void UWebsocketSampleSubsystem::OnSocketConnectioinError(const FString& err) const
{
UE_LOG(LogMyWebSocket, Warning, TEXT("OnSocketConnectioinError: %s"), *err);
}
void UWebsocketSampleSubsystem::OnSocketClosed(const int statusCode, const FString& reason, const bool wasClean) const
{
UE_LOG(LogMyWebSocket, Log, TEXT("OnSocketClosed: statuscode %d, reason %s, wasClean %s"), statusCode, *reason, (wasClean ? TEXT("true") : TEXT("false")));
}
void UWebsocketSampleSubsystem::OnSocketMessageReceived(const FString& msg)
{
UE_LOG(LogMyWebSocket, Log, TEXT("OnSocketMessageReceived: %s"), *msg);
currentMsg = msg;
OnReceiveSockMsg.Broadcast(currentMsg);
}