Server-Client Communication in Unity

Server-Client Communication in Unity

#gamedevelopment?,?#unity3d?,?#csharp , #digitaltwin , #3d , #programming


Foreword

Hello and welcome back. This week I want to finish my little digression on the Chain of Responsibility design pattern and the ConcurrentQueue data structure. That means it's time to implement a simple TCP client and server.

Abstract

Listening to client and server messages is usually done via a separate thread so that the main thread is not blocked. Unfortunately, Unity is not thread-safe and calling the API from other threads was made impossible.

One solution to this problem is the combination of the Chain of Responsibility pattern and the ConcurrentQueue data structure.

Preparation

To keep the code as short as possible, we will use an existing library called SimpleTCP in our Unity project.


(Skip this section if you already know how to get Nuget packages and how to add DLLs in Unity projects)


Steps to Reproduce

(1) Create a new .Net project in Visual Studio.

(2) Depending on your platform, you may be able to select the target framework during creation. However, you can also change the target framework later by right-clicking the project and selecting Options. A window like the one in Figure 1 should now open. Here you can make the changes to the target platform.

Es wurde kein Alt-Text für dieses Bild angegeben.
Figure 1

(3) Select .Net Framework 4.5 and close the window afterwards.

(4) Right-clicking the project and selecting Manage Nuget Packages... will open another window, as shown in Figure 2. Search for SimpleTcp in the search bar and select Add Package.

Es wurde kein Alt-Text für dieses Bild angegeben.
Figure 2

(5) Now build your empty project once. Either in Debug or in Release mode. Afterwards you will find a file called SimpleTCP.dll in the bin/Debug or bin/Release folder of your project.

(6) Now copy the DLL into your Unity project. E.g. under Assets/Plugins folder or similar.

Design

For the communication between client and server, we first need some data classes. For better comprehensibility, we use the classes from article 6 again. But now they have some attributes and some properties. We will also reuse the base class from our Chain of Responsibility pattern (see Figure 3).

Es wurde kein Alt-Text für dieses Bild angegeben.
Figure 3

Next, we need two new data processing classes that will later represent our Chain of Responsibility (see Figure 4).

Es wurde kein Alt-Text für dieses Bild angegeben.
Figure 4

Now we can start implementing a TCP server. Thanks to the SimpleTcpServer class, our own class remains very clear (see Figure 5).

Es wurde kein Alt-Text für dieses Bild angegeben.
Figure 5

The same applies to our TCP client class (see Figure 6).

Es wurde kein Alt-Text für dieses Bild angegeben.
Figure 6

Last but not least, we need a central point where we can test our implementation (see Figure 7). For simplicity, this is all done in a single MonoBehaviour.

Inside the Start method, we ...

  1. ... initialize a ConcurrentQueue
  2. start a TCP server in another thread
  3. and connect two TCP clients to the server, which then repeatedly send messages from our main thread to our server.

In the Update method, the received server messages are then finally consumed.

Es wurde kein Alt-Text für dieses Bild angegeben.
Figure 7

Final Words

In real-world use cases, the TCP server would likely be an independent application. The Unity application, on the other hand, would act as a TCP client that connects to the server and waits in a separate thread for incoming messages from the server.

Since the messages are passed to the main thread via the ConcurrentQueue before or after processing, the Unity API can also be called afterwards. E.g. to update real-time data in a dashboard.


That's it for today.

What do you think about this approach? Have you ever had such a use case? Or do you think that I have forgotten an important aspect, then please leave a comment.

And if you are interested in topics like game development, project organization or software design patterns, don't forget to press the Follow button on?my profile page?or add me as a contact. Not convinced yet? Well, maybe you will be once you read some of my?other articles.

要查看或添加评论,请登录

Lars Wobus的更多文章

  • Brief introduction of my main game mechanics

    Brief introduction of my main game mechanics

    As some readers of my newsletter already know, I'm currently developing a turn-based strategy game, among many other…

  • UI & UX design from a Programmer's Perspective

    UI & UX design from a Programmer's Perspective

    As some people already know, I'm currently developing a strategy game as part of my second master's degree at the…

  • Asset and Project Management for Indie Game Developers

    Asset and Project Management for Indie Game Developers

    This week, I want to talk about Miro, which my tutors and comillitons recommended to me some time ago. It's an online…

  • Unfinished Unity Projects (Round 2)

    Unfinished Unity Projects (Round 2)

    #Unity #3D #Blender Foreword Welcome back to episode 20 of my newsletter. For this particular episode, I thought we'd…

  • Greyboxing in Blender

    Greyboxing in Blender

    Hello fellow game devs and hobbyists and welcome back to the next episode of my newsletter. This time I will talk about…

  • The importance of a core gameplay loop

    The importance of a core gameplay loop

    The need for a good gameplay loop can be summed up in one sentence. If the main game mechanics are flawed or not…

    1 条评论
  • Data Analysis before, during and after Live Events

    Data Analysis before, during and after Live Events

    #marketing - #games - #gaming - #gamedevelopment - #videogames On Friday, April 14, 2023, I visited the game festival…

  • Paper Prototyping in Game Development

    Paper Prototyping in Game Development

    Foreword Last week one of the instructors at the University of Applied Science in Dresden said that he insists that I…

  • Data Driven Marketing - About Touchpoints and Customer Journey

    Data Driven Marketing - About Touchpoints and Customer Journey

    Foreword In the past, it was enough to advertise new products widely to boost sales. But that is no longer sufficient!…

  • Market Research for Game Developers

    Market Research for Game Developers

    Foreword Only a few people already know that I have started a second master study on the subject of Games and XR…

社区洞察

其他会员也浏览了