Networking - Advanced

From Spheriki

Jump to: navigation, search

There was a great deal of conversation and headaches that came from trying to make Massively Multiplayer Online Game with Sphere. I thought I would try and make this page to clear things up for everyone.

This tutorial will start from the ground up, and you do not need any prior Sphere networking experience to use it, but you do need experience writing in JavaScript, and be familiar with Sphere's functions.

Contents

General Info

There are some basic things you need to know before you get started. For the purposes of this tutorial, it is assumed that all information is correct.

  1. A Client is a computer that connects to a server, and interacts with other clients through it.
  2. A Server is a computer that receives connections from the clients, and handles communication between them.
  3. An IP Address is a unique number, separated by three decimal points (such as 100.200.300.400), that identifies each computer connected to a network. Every computer has a different, unique IP Address. Through the use of IP Addresses, a client can connect to a specific server.
  4. A Port is like a door. When it is open, connections can be formed through it, and when it is closed, connecting through it is impossible. Servers must listen on ports, thus opening the 'door,' to receive a connection from a client. Their are 65536 ports altogether, numbered from 0 to 65535, but only a single port is needed for multiple connections.
  5. A Socket Object represents the connection between the server and client. Data may be sent and received by both computers through the use of a socket.

The Socket

The socket is an object that represents connected data on a certain port. There can be multiple sockets on the same port, each supplying four key parts:

  • The source IP,
  • The source port,
  • The destination IP and,
  • The destination port.

The source port and destination port should be the same. The source is equal to the computer sending data and the destination, well, the place where the data is headed, in this case a server. The idea behind banning an IP address is to take the source IP and compare it with a list of IP's that need to be ignored, if so, close that socket.

Server - Client Relationship

The purpose of a server is soley so the clients never connect to each other, they only need to connect to one thing. Since the clients are never directly connected, they must need to interact through the server, which can also handle things such as accounts, and control any rules or regulations for the application. When a client sends data to a server, the server interprets it, and then uses it according to the situation (sending chat messages to other clients, updating player locations, etc.). In this way, server acts as a medium for communication between the players.

To make an MMO

To make an MMO you basically need put the above elements to use. You must design two different scripts, a Server script, and a Client script. The client sends it's data (Chat messages,X and Y positions, etc.) to the server, and the server sends the data to all of the other clients, so everyone has everyone's data. We will start with a basic server script, though usually you will make the server and client scripts simultaneously.

Server Scripting

The server script must first set be set up to "listen" for clients...

var port=1234;                         //This is the port number that we use.
var sockets=new Array();               //This is the array for the connections we have made.
var socketattempt=ListenOnPort(port);  //This is how we listen for a new client.

function ListenForClients()
{
  if (socketattempt.isConnected())     //this checks to see if the socket has been connected
  {
    sockets.push(socketattempt);       //adds the connected socket to the 'sockets' array.
    socketattempt=ListenOnPort(port);  //continues listening for more clients
  }
}

function game()
{
  while (!IsKeyPressed(KEY_ESCAPE))    //the main Server loop
  {
    ListenForClients();                //This listens for new clients
                                       //Since the server constantly listens, it is in the main loop.
    //Here is where the server interacts with all of the clients, and handle the messages.
  }
}

Now what this does, is it listens on port 1234 for clients. When a client script tries to connect to the server with the correct IP Address and port number, the socket becomes connected, and is added to the sockets array. Then the server continues listening on the same port for more clients.

Client Scripting

And now for the client script... We want to be able to connect to the server, so let's pretend the server is using the above script, and the server's IP Address is 123.123.123.123.

var socket;

function game()
{
  socket=OpenAddress("123.123.123.123",1234) //Attempts to connect to the server
  while (!socket.isConnected())
  {
     // This is where we wait for the connection to establish.
     // You might have a message saying that you are connecting,
     // or maybe even make a connection failure timeout.
     // The client only makes one connection, so it is not in the
     // main loop.
  }
  while (!IsKeyPressed(KEY_ESCAPE))    //the main Client loop
  {
     //Here is where the client interacts with the server.
  }
}

Conclusion

I certainly hope that this explaination proves worthwhile to any MMO's that are made in Sphere. Please keep in mind that this is intentionally a rough explanation, so it may be used as a basic outline for any multiplayer application. A suggestion I have would be to use Json, which is an easy-to-use script that can greatly simplify sending multiple values over a network connection. If you have any questions, you can email me and I will get back to you whenever possible.

Written by: Leon

Personal tools