Multiplayer game using sockets

Hi guys! I’m i’m going to graduate this year and I’ll have to make a graduation project so I was thinking to use unity, which I already more-or-less know (I have already made some games). This will be a two-players multiplayer game, and I was wondering how could I make the networking part work. Since this is a school project I’d rather not use networking tools which Unity provides, I would prefer to write myself lower level networking functions (maybe with sockets). I will have to sync positions of game objects in real-time (like one would do in a online fps). Should I use UDP? On which interval should I sync the objects’ position? And how should I design the message passed between the clients?
I’m completely new to networking programming (I only made a chat program with c# and TCP sockets, just for testing), so if you could answer those questions or if you had some advice about multiplayer programming patterns I would be very grateful :slight_smile:
Anyway, thank you for your time!

If you want to use sockets and the LLAPI then you will probably want to look up NetworkTransport which uses UDP, it seems that the LLAPI which uses Sockets is only UDP, I couldn’t find any TCP for that library.

As far as I can see it does some stuff already out of the box behind the scenes, but just try the examples.
Also once the client has connected to the server / other host anything you send with NetworkTransports.Send(…) will be flagged as DataEventType.

I have only played with it for the last few days and it seems very easy to get into, just a shame there isn’t a better documentation of what it does itself so that we know what we don’t need to add.

But overall it seems very good so far.

If its going to be a P2P game where one of the players host the game, then you might also want to look up NetworkTransport.StartBroadcastDiscovery(…) which if I am correct is used by the client to send out a network packet asking for the server / host for its network information so it can connect to it.

But yeah, take a look at NetworkTransport, that should get you started.

In case of low level network programming, you need to make reliable udp connection somehow. Or you could try websockets, I think it has such udp connection already.

You can sync your position as fast as you need. You can sync by command in case of top-down mmorpg, or sync 60 times per second in case of fast-paced shooter.

For messages you can send serialized c# objects or json strings via your connection.

@shoo
Are you referring to HTTP(s) POST and GET WebRequests ?
Basically this: Unity - Scripting API: UnityWebRequest

GET and POST Requests will not be any good if you are syncing movements of objects, this is due to there being loads of requests per second for each player and the webserver will get bogged down.

If you are wanting to sync objects and handle them moving then you can either use the HLAPI with their Match Making and Relay Services which has a limitation depending on how much you want to spend.

Or you can go the LLAPI route and code everything yourself that way you have more control over what its doing.

Or have I missed stuff.

You should know this resource:
http://gafferongames.com/networking-for-game-programmers
Use this as foundation and then experiment, if you want to do low level networking stuff.

1 Like

It’s a little unclear whether you are using websockets or UDP sockets, as you mention both, so lets clarify a few things first.

UDP is a protocol that you can use in pretty much any application to easily create a bi-directional network connection between two peers (most likely a client and a server). Websockets are similar, in that they are also designed to create a bi-directional connection between two peers, however websockets are designed to be used in web applications (applications that are executed within a web browser, such as a website or an embedded web browser game). The reason websockets exist is because UDP - which is generally a better choice of protocol for networking - cannot be used in web applications. Websockets, however, can.

As a less-important note (and since you mentioned TCP), websockets are just an “upgraded” http connection that remains persistent so that data can be transmitted on-demand between two peers. So it essentially uses TCP.

So firstly, if you are building a game in unity where you want to be able to build the game to the WebGL platform (so that it can be played in a browser) then you WILL need to support websockets. If you have no intention to build to WebGL, avoid websockets entirely, and just focus on UDP. You will always get more performance and flexibility with UDP than you will with websockets, so if you have the option, use UDP.

Moving on. You mention that you want to build your own networking solution, however you also mention that you have little experience in networking. I absolutely implore you to go ahead and try and build something from scratch if you are keen to learn, but be prepared to go bald, and to make a hell of a lot of mistakes. Networking is probably the second most challenging topic in programming, behind multi-threaded programming, so if you think you have what it takes, then go for it (that’s what I did!)

However, a few have mentioned the use of the LLAPI as a starting point, and I also recommend the same. The LLAPI is very much a low-level api, in that it provides a very basic foundation for you to build from, without having made too many decisions for you. It tackles multi-threading issues, and various other challenges (such as pooling, buffering, throttling etc) for you. Most of which aren’t too important to understand yourself.

With that being said, and going back to the topic of websockets, the LLAPI currently does not support websockets on the client side. It is likely that this will change over time. However, if you require websocket support both on the client AND the server then you will need to use a few of the HLAPI elements, such as the NetworkClient and NetworkServerSimple class. I wrote about this in great detail here: Client-side websocket support with the llapi? - Unity Engine - Unity Discussions

Good luck with your game!

2 Likes