Connect to Chat Server
Connect to the chat server.
Method Information
| Item | Content |
|---|---|
| Method | AChatManager::GetInstance()->Connect(IChatListener* Listener) |
| Callback | OnConnected() - Called on connection success |
Code Example
// IChatListener implementation class
void UMyGame::ConnectToChat()
{
AChatManager::GetInstance()->Connect(this);
}
void UMyGame::OnConnected()
{
UE_LOG(LogTemp, Log, TEXT("Chat Connected"));
AChatManager::GetInstance()->Subscribe(TEXT("CH01"));
}
void UMyGame::OnDisconnected()
{
UE_LOG(LogTemp, Log, TEXT("Chat Disconnected"));
}
void UMyGame::OnError(const FString& Code, const FString& Message)
{
UE_LOG(LogTemp, Error, TEXT("Chat Error: [%s] %s"), *Code, *Message);
}
void UMyGame::OnPublicMessage(const FChatUserInfo& Sender, const FString& Message)
{
UE_LOG(LogTemp, Log, TEXT("[%s] %s"), *Sender.visitorName, *Message);
}
void UMyGame::OnPrivateMessage(const FChatUserInfo& Sender, const FString& Message)
{
UE_LOG(LogTemp, Log, TEXT("[Whisper from %s] %s"), *Sender.visitorName, *Message);
}
void UMyGame::OnNotifyMessage(const FChatUserInfo& Sender, const FString& Message)
{
UE_LOG(LogTemp, Log, TEXT("[System] %s"), *Message);
}
void UMyGame::OnSubscribed(const FChatUserInfo& User)
{
UE_LOG(LogTemp, Log, TEXT("%s entered"), *User.visitorName);
}
void UMyGame::OnUnSubscribed(const FChatUserInfo& User)
{
UE_LOG(LogTemp, Log, TEXT("%s left"), *User.visitorName);
}
void UMyGame::OnChannels(const TArray<FChatChannelInfo>& Channels)
{
for (const auto& Channel : Channels)
{
UE_LOG(LogTemp, Log, TEXT("Channel: %s, Users: %d"), *Channel.channel, Channel.count);
}
}
void UMyGame::OnPlayerOnline(const TArray<FChatPlayerInfo>& Players)
{
for (const auto& Player : Players)
{
UE_LOG(LogTemp, Log, TEXT("Player: %s, Online: %s"), *Player.userUniqueId, *Player.online);
}
}
IChatListener Implementation Required
To use chat features, the class must implement the IChatListener interface.
Known Issues
Unreal Engine WebSocket Path Bug
Unreal Engine's libwebsockets library has a bug where it requests // when there is no path in the URL. This may cause connection failures or immediate termination with some WebSocket servers.
Symptoms:
- WebSocket connection terminates immediately after success
OnClosedcallback is called with abnormal close code- Unity connects to the same server normally but Unreal fails
Solution:
Explicitly add / path to the WebSocket URL.
// Incorrect example (bug may occur)
FString Url = TEXT("wss://example.com:3001?param=value");
// Correct example (explicit '/' path)
FString Url = TEXT("wss://example.com:3001/?param=value");
Reference
This bug occurs in Unreal Engine 5.x's libwebsockets implementation. The PlayNANOO SDK handles this issue internally, so no additional action is needed when using the SDK.