Coder Social home page Coder Social logo

breathxhe / ue4tcpsocketplugin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from codespartan/ue4tcpsocketplugin

0.0 1.0 0.0 227 KB

Tcp Socket Plugin facilitates communication with a TCP server in blueprints or in code.

License: MIT License

C++ 95.78% C# 4.22%

ue4tcpsocketplugin's Introduction

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.

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, Float, 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

Usage in C++

This is only an example. In .h

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<uint8>& Message);
  
  	UFUNCTION(BlueprintCallable)
	void ConnectToGameServer();
	
	UPROPERTY()
	int32 connectionIdGameServer;
}

In .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<uint8>& 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<uint8> 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

ue4tcpsocketplugin's People

Contributors

codespartan avatar omeyer973 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.