8 Protocol draft 2.0
jeena edited this page 2011-02-11 07:09:15 -08:00

The 2.0 specification is quite different from the old one. This one is based on a stable and mature protocol; HTTP. We will borrow certain elements from the protocol, with main focus on HTTP's POST style. Here is a link: http://www.jmarshall.com/easy/http/#http1.1c1 Below is a typical POST message in HTTP.

POST /path/script.cgi HTTP/1.0
From: frog@jmarshall.com
User-Agent: HTTPTool/1.0
Content-Encoding: application/x-www-form-urlencoded
Content-Size: 32

home=Cosby&favorite+flavor=flies

Our protocol is, unlike HTTP, asynchronous and statefull. After initializing a socket you send the hello server command:

Server-Command: hello
User-Agent: Some Game v 0.2
Accept: plaintext

You will get an answer with your own client token:

Client-Command: welcome
Content-Encoding: plaintext
Content-Size: 45
GGS-Version: 1.0
Accept: gzip,plaintext

913AB7FD-F795-45E4-B31C-035FD81D0773

Then you will define your game, that means, you send the source code of your game to the server:

Token: 913AB7FD-F795-45E4-B31C-035FD81D0773
Server-Command: define
Content-Encoding: plaintext
Content-Type: javascript
Content-Size: 666

function userCommand(user, cmd, args) {
	if(cmd == "moveUp") {
		moveUp(user, args);
	} else if(cmd == "moveDown") {
		moveDown(user, args);
	} else if(cmd == "chat") {
		chat(user, args);
	}
}

function moveUp(user, x) {
	var key = "userpos_x_" + user.id;
  var value = GGS.world.get(key);
	value += x;
	GGS.world.set(key, value);
}

function moveDown(user, x) {
	var key = "userpos_x_" + user.id;
  var value = GGS.world.get(key);
	value -= x;
	GGS.world.set(key, value);
}

function chat(user, text) {
	var size = GGS.users.length;
	for(var i = 0; i < size; i++) {
		var user = GGS.users.get(i);
		user.sendCommand("chat", text);
	}
}

Now you can call commands in that game:

Token: 913AB7FD-F795-45E4-B31C-035FD81D0773
Command: moveUp
Content-Encoding: plaintext
Content-Size: 1

5

or:

Token: 913AB7FD-F795-45E4-B31C-035FD81D0773
Command: chat
Content-Encoding: plaintext
Content-Size: 90

Hi guys,

My name is Mimi and I'm new here. Would be great to here some of
your stories.

If you are not the first client who connects to the server in this game session, then you will have got a game token which will tell the server to which game you would like to connect (you get the game token from your friends or from the game lobby).

Server-Command: hello
Game-Session-Token: 018FF079-B383-4523-93A2-6053E7EDC2BC
User-Agent: Some Game v 0.2
Accept: zip,plaintext

Here you would again get an answer with your own Client token.

If you are already connected to a game and would like to obtain the game token you send the command game-session-token:

Token: 913AB7FD-F795-45E4-B31C-035FD81D0773
Server-Command: game-session-token
User-Agent: Some Game v 0.2
Accept: plaintext

You will get as an answer just the game session token:

Client-Command: game-session-token
Content-Encoding: plaintext
Content-Size: 45
Accept: gzip,plaintext

018FF079-B383-4523-93A2-6053E7EDC2BC