Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/CodeSpartan/UE4TcpSocketPlugin
Tcp Socket Plugin facilitates communication with a TCP server in blueprints or in code.
https://github.com/CodeSpartan/UE4TcpSocketPlugin
Last synced: about 1 month ago
JSON representation
Tcp Socket Plugin facilitates communication with a TCP server in blueprints or in code.
- Host: GitHub
- URL: https://github.com/CodeSpartan/UE4TcpSocketPlugin
- Owner: CodeSpartan
- License: mit
- Created: 2019-04-15T14:14:07.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-04-25T16:51:26.000Z (8 months ago)
- Last Synced: 2024-08-02T16:31:06.507Z (4 months ago)
- Language: C++
- Homepage:
- Size: 643 KB
- Stars: 247
- Watchers: 15
- Forks: 72
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-unreal - UE4TcpSocketPlugin - Tcp Socket Plugin facilitates communication with a TCP server in blueprints or in code. (Networking)
README
# UE4 TCP Socket Plugin
Tcp Socket Plugin for Unreal Engine 4 facilitates communication with a TCP server purely in blueprints. Client-only functionality. Built version can be downloaded [here](https://unrealengine.com/marketplace/en-US/product/tcp-socket-plugin).# List of Features
- Multiple connections
- Multi-threading: each connection runs on its own thread
- Detects disconnects as soon as they happen
- Event dispatchers: OnConnected, OnDisconnected (also triggers when connection fails), OnMessageReceived
- Serialize and deserialize basic types: UInt8, Int32, Int64, Float (4 and 8 bytes), String
- Free and open source under MIT license# Usage in Blueprints
Create a blueprint actor inheriting from TcpSocketConnection, drop it into level and use these nodes:
![Alt text](/functionality.jpg?raw=true "Functionality")# Usage in C++
This is only an example.
In .h
```cpp
UCLASS()
class EXAMPLE_API ACppSocketConnection : public ATcpSocketConnection
{
GENERATED_BODY()
public:
UFUNCTION()
void OnConnected(int32 ConnectionId);UFUNCTION()
void OnDisconnected(int32 ConId);UFUNCTION()
void OnMessageReceived(int32 ConId, TArray& Message);
UFUNCTION(BlueprintCallable)
void ConnectToGameServer();
UPROPERTY()
int32 connectionIdGameServer;
}
```In .cpp
```cpp
void ACppSocketConnection::ConnectToGameServer() {
if (isConnected(connectionIdGameServer))
{
UE_LOG(LogError, Log, TEXT("Log: Can't connect SECOND time. We're already connected!"));
return;
}
FTcpSocketDisconnectDelegate disconnectDelegate;
disconnectDelegate.BindDynamic(this, &ACppSocketConnection::OnDisconnected);
FTcpSocketConnectDelegate connectDelegate;
connectDelegate.BindDynamic(this, &ACppSocketConnection::OnConnected);
FTcpSocketReceivedMessageDelegate receivedDelegate;
receivedDelegate.BindDynamic(this, &ACppSocketConnection::OnMessageReceived);
Connect("127.0.0.1", 3500, disconnectDelegate, connectDelegate, receivedDelegate, connectionIdGameServer);
}void ACppSocketConnection::OnConnected(int32 ConId) {
UE_LOG(LogTemp, Log, TEXT("Log: Connected to server."));
}void ACppSocketConnection::OnDisconnected(int32 ConId) {
UE_LOG(LogTemp, Log, TEXT("Log: OnDisconnected."));
}void ACppSocketConnection::OnMessageReceived(int32 ConId, TArray& Message) {
UE_LOG(LogTemp, Log, TEXT("Log: Received message."));
// In this example, we always encode messages a certain way:
// The first 4 bytes contain the length of the rest of the message.
while (Message.Num() != 0) {
// read expected length
int32 msgLength = Message_ReadInt(Message);
if (msgLength == -1) // when we can't read 4 bytes, the method returns -1
return;
TArray yourMessage;
// read the message itself
if (!Message_ReadBytes(msgLength, Message, yourMessage)) {
// If it couldn't read expected number of bytes, something went wrong.
// Print a UE_LOG here, while your project is in development.
continue;
}
// If the message was read, then treat "yourMessage" here!
// ...
// And then we go back to the "while", because we may have received multiple messages in a frame,
// so they all have to be read.
}
}
```# Platforms
Intended for all platforms that support sockets and multithreading, which is most of them, except HTML5.
Tested on platforms: Windows