Rewritten the protocol

Updated Protocol draft 2.0 (markdown)
jeena 2011-02-11 06:29:29 -08:00
parent c3873acd40
commit f3d7dadce7

@ -1,22 +1,111 @@
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
<pre>POST /path/script.cgi HTTP/1.0
From: frog@jmarshall.com
User-Agent: HTTPTool/1.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 32
<BLANK LINE>
home=Cosby&favorite+flavor=flies
Our protocol looks like the following:
Token: 1001
Command: define
home=Cosby&favorite+flavor=flies</pre>
Our protocol is, unlike HTTP, asynchronous and statefull. After initializing a socket you send the hello server command:
<pre>Server-Command: hello
User-Agent: Some Game v 0.2
Accept: plaintext</pre>
You will get an answer with your own client token:
<pre>Server-Command: token
Content-Type: plaintext
Content-Length: 45
Server-Version: 1.0
Accept: gzip,plaintext
913AB7FD-F795-45E4-B31C-035FD81D0773</pre>
Then you will define your game, that means, you send the source code of your game to the server:
<pre>Token: 913AB7FD-F795-45E4-B31C-035FD81D0773
Server-Command: define
Content-Type: text
Content-Length: 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);
}
}</pre>
Now you can call commands in that game:
<pre>Token: 913AB7FD-F795-45E4-B31C-035FD81D0773
Command: moveUp
Content-Type: text
Content-Length: 40
<BLAND LINE>
function helloWorld(x) {return x + 2 ;}
The following commands are to be supported:
define defines new javascript
run runs existing javascript
hello request a new token
5</pre>
or:
<pre>Token: 913AB7FD-F795-45E4-B31C-035FD81D0773
Command: chat
Content-Type: text
Content-Length: 90
Hi guys,
My name is Mimi and I'm new here. Would be great to here some of
your stories.</pre>
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).
<pre>Server-Command: hello
Game-Session-Token: 018FF079-B383-4523-93A2-6053E7EDC2BC
User-Agent: Some Game v 0.2
Accept: zip,plaintext</pre>
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:
<pre>Token: 913AB7FD-F795-45E4-B31C-035FD81D0773
Server-Command: game-session-token
User-Agent: Some Game v 0.2
Accept: plaintext</pre>
You will get as an answer just the game session token:
<pre>Server-Command: game-session-token
Content-Type: plaintext
Content-Length: 45
Server-Version: 1.0
Accept: gzip,plaintext
018FF079-B383-4523-93A2-6053E7EDC2BC</pre>