Client-Server Communication Socket-Based in Videogames – Basic Concepts
What is a Client-Server Communication
Client-server is a request-response remote communication model that involves processes requesting services from other processes which offer these services via the network.
The processes offering services by executing certain tasks following remote process requests are known as servers. In general, the servers receive requests from remote processes, execute the tasks associated with these services, and dispatch responses back to the requesting entities. Example of services include database information retrieval and updates, file system access services, and dedicated user-application tasks.
The process that contact the servers and request them to perform services is known as Client
In general, Client processes manage user-interfaces, validate data entered by users, dispatch requests to servers, collect servers’ responses, and/or display the information received.
Client-Server Communication in Videogames
In the fantastic world of videogames development we find:
Client
It is the videogame, installed in your PC, Mobile Device or Web Player, generally:
– manage the user input
– manage local data as scores, lives etc…
– collect data from a database installed inside a Server and manage it inside the videogame
Server
It is the computer that stores (using for example MySQL) accounts, scores and others usefull datas for the gamer
If you play a MMORPG (Massive(ly) Multiplayer Online Role-Playing Game, you will see that the Server manages a lot af data and thousands of users in the same time.
Low Data Management
If you have to manage few users or only username and scores value, the server will not work hard, it is enough a communication like that:
Client (Videogame) -> PHP POST REQUEST -> Server with PHP+MySQL offers a service
High Data Management
You are in trouble, there are thousands of users that sends thousands of PHP POST in the same time.
Using the previus scheme we have:
Client1 (Videogame) -> PHP POST -> Server with PHP+MySQL offers a service
Client2 (Videogame) -> PHP POST -> Server with PHP+MySQL offers a service
Client3 (Videogame) -> PHP POST -> Server with PHP+MySQL offers a service
Client4 (Videogame) -> PHP POST -> Server with PHP+MySQL offers a service
Client5 (Videogame) -> PHP POST -> Server with PHP+MySQL offers a service
Client6 (Videogame) -> PHP POST -> Server with PHP+MySQL offers a service
Client7 (Videogame) -> PHP POST -> Server with PHP+MySQL offers a service
Client8 (Videogame) -> PHP POST -> Server with PHP+MySQL offers a service
Client9 (Videogame) -> PHP POST -> Server with PHP+MySQL offers a service
Client1000000 (Videogame) -> PHP POST REQUEST -> Server with PHP+MySQL offers a service …-> CRASH OF THE SYSTEM!!!
The solution are:
1. Use of SSD (Solid State Drive) instead ad traditional Hard Driver
2. Use of one or more dedicated servers, not sharing IP or low-end hosting solution admitted!
3. Use of Client-Server Communication Socket-Based
The Client-Server Communication Socket-Based is:
Client1 (Videogame) -> PHP POST -> | |
Client2 (Videogame) -> PHP POST -> | |
Client3 (Videogame) -> PHP POST -> | |
Client4 (Videogame) -> PHP POST -> | |
Client5 (Videogame) -> PHP POST -> | Socket manage REQUEST | -> Server with PHP+MySQL offers a service
Client6 (Videogame) -> PHP POST -> | (Multithreaded) |
Client7 (Videogame) -> PHP POST -> | |
Client8 (Videogame) -> PHP POST -> | |
Client9 (Videogame) -> PHP POST -> | |
With WebSockets you can transfer as much data as you like without incurring the overhead associated with traditional HTTP requests. Data is transferred through a WebSocket as messages, each of which consists of one or more frames containing the data you are sending (the payload).
To run a WebSocket you need a DEDICATED IP, no SHARED IP are allowed, because you will setup your own IP and listening ports.
A popular software to create a WebSocket is Node.js (http://nodejs.org/).
WebSoscket inside a SHARED IP
You can run Node.js in a shared host using CGI-Node (http://www.cgi-node.org/).
‘CGI-Node’ is a CGI adaptor for node.js that mimmicks the Node.js http library.
Low Latency Client-Server and Server-Client Connections
Around 2005, AJAX started to make the web feel more dynamic. Still, all HTTP communication was steered by the client, which required user interaction or periodic polling to load new data from the server.
One of the most common hacks to create the illusion of a server initiated connection is called long polling. With long polling, the client opens an HTTP connection to the server which keeps it open until sending response. Long polling and the other techniques work quite well. You use them every day in applications such as GMail chat.
However, all of these work-arounds share one problem: They carry the overhead of HTTP, which doesn’t make them well suited for low latency applications. Think multiplayer first person shooter games in the browser or any other online game with a realtime component.
The WebSocket (http://dev.w3.org/html5/websockets/) specification defines an API establishing “socket” connections between a web browser and a server. In plain words: There is an persistent connection between the client and the server and both parties can start sending data at any time over a single TCP channel.
WebSocket give you a bidirectional communication technology for web apps for:
IE 10 or over
Firefox 33 or over
Chrome 31 or over
Safari 7.1 or over
Opera 26 or over
iOS Safari 7.1 or over
Android Browser 4.4 or over
Chrome for Android 39 or over
What it means single TCP Channel?
The Transmission Control Protocol (TCP) is one of the core protocols of the Internet protocol suite (IP), and is so common that the entire suite is often called TCP/IP.
IP works by exchanging pieces of information called packets. A packet is a sequence of bytes and consists of a header followed by a body. The header describes the packet’s source, destination and control information. The body contains the data IP is transmitting. The size of one IP packet is 65535 bytes (63.99902 Kbyte or 0.0625 Mbyte).
Due to network congestion, traffic load balancing, or other unpredictable network behavior, IP packets can be lost, duplicated, or delivered out of order. TCP detects these problems, requests retransmission of lost data, rearranges out-of-order data, and even helps minimize network congestion to reduce the occurrence of the other problems.
TCP is utilized extensively by many of the Internet’s most popular applications, including the World Wide Web (WWW), E-mail, File Transfer Protocol, Secure Shell, peer-to-peer file sharing, and some streaming media applications.
TCP is optimized for accurate delivery rather than timely delivery, and therefore, TCP sometimes incurs relatively long delays (on the order of seconds) while waiting for out-of-order messages or retransmissions of lost messages. It is not particularly suitable for real-time applications such as Voice over IP. For such applications, protocols like the Real-time Transport Protocol (RTP) running over the User Datagram Protocol (UDP) are usually recommended instead.
Ready to use solutions
Setup a server is very expensive, sometimes the budget and the time we can spend to develop is not enought to do that.
In the market there are some ready to use platforms to build multiplayer games.
Photon Cloud
https://www.exitgames.com
Photon Cloud is available globally to guarantee low latency and shortest round-trip times for your multiplayer games worldwide.
Distributed worldwide Photon Cloud scales seamless and automatically even for tens of thousands of concurrent users.
Customers: from indie to AAA
Support:
– Unity3D (check Unity3D Asset Store also)
– iOS
– Android
– Win8
– Unreal4
– Cocos
– HTML5
– Flash
– Marmalade
– Corona
– Realtime Multiplayer
– Turn Based Games
– Chat
App Warp
http://appwarp.shephertz.com/
Powerful, yet simple to use cloud platform for massively multiplayer realtime games
Customers: from indie to AAA
Support:
– Unity3D
– iOS
– Android
– Win8
– Unreal4
– Cocos
– HTML5
– Flash
– Marmalade
– Corona
– J2me SDK
– andengine
– Starlink SDK
– Fixel SDK
– Sencha SDK
Game Cloud
http://game-cloud.org/
Using our advanced technology we reduce the traffic to the player allowing large number of players to connect even on slow (mobile) networks without lagging
Support:
– Unity3D (Build for Unity3D integrated into Unity Editor Tools)
– Win 8
– Android
– Mac OS
– Linux
– Playstation
– XBox 360
– Wii
– iOS
– Browsers
Gamooga
http://www.gamooga.com/
State-of-art realtime backend for your apps/games that guarantees under 1ms response time. None of our competitiors can beat us here!
Support:
– Unity3D
– HTML5
– iOS
– Android
– Falsh
– Marmalade
Gamespark
http://www.gamesparks.com/
GameSparks ensures you keep your costs to a minimum whether they be intial set-up or recurring fees
Support:
– Unity3D
– Unreal 4
– iOS
– Android
– Falsh
– Marmalade
My official website: http://www.lucedigitale.com
References:
– Server Side Score
http://www.lucedigitale.com/blog/unity-3d-server-side-highscores-js-programming/
– Advanced Network Programming – Principles and Techniques (By Bogdan Ciubotaru, Gabriel-Miro Muntean)
– http://www.html5rocks.com/en/tutorials/websockets/basics/ (By Malte Ubl and Eiji Kitamura)
– http://caniuse.com/#feat=websockets