About
Apr0
The Concept
Weasel is a framework for building a rich-internet-applications with a comet-service also called “ajax push”. Unlike other comet implementations the weasel project goes a very differnt way.
The weasel framework dont use push or pull. It use a simple socket stream. It creates a socket connection between a server and the browser. The server and the client communicate via a command protocol, which is a simple json format.
The Implementation
The current available browser dont have the capability to create a direct socket connection. Maybe in the future but for now there is no chance. So what we use is a little helper technology called “Flash“.
Flash has the functionality to create socket connection to a remote server. But we dont want to write our whole applications in flash. We use flash only for the connection and pass the commands from the server to javascript.
The Details
Server side
First of all we have a Server written in PHP which creates a socket. The server handels all the connected clients and has a command interface.
// server.php class MyServer extends Weasel_Server { } MyServer::run();
Now you can implement commands, which can be called by the clients.
class MyServer_Move extends Weasel_Command_Abstract { public function execute($params) { return array( 'x' => 5, 'y' => 5); } }
The server loads all commands at start. On default the result is send to every connected client.
Client side
Now we can call from the client via javascript this command ( a request command )
Weasel.Application = "MyServer" Weasel.ready(function() { Weasel.command('move', {x : 5, y: 7}, function(params) { console.log(params.x); console.log(params.y); }) })
This is the process in detail:
- A javascript methode puts the command with an id in the request stack
- JSON.stringify encodes the command to JSON
- The json string is pass to the flash client
- The Flash client writes the json string in the socket stream
- The PHP server reads from the socket stream and detecting a command request
- The PHP server delegates the command to the right class and call the execute methode of the class
- The return value encodes to JSON
- The PHP server writes the json string in the socket stream
- The Flash client reades from the stream, detects a response and pass the string to the dispatch Javascript methode
- JSON.parse decodes the JSON string to a javascript object
- If the command object is a response, the dispatcher is looking up in the request stack for the right request (ID)
- The dispatcher calls the callback methode
The sender client get a response command from the server. If the return of the command is send to every client, then the other clients get a request command from the server.
You can register requests commands via javascript:
var move = function(params) { console.log(params.x); console.log(params.y); } Weasel.register('move', move);
In short term:
- The sender clients gets a response command and will call the callback methode from the request stack
- All other clients gets a request command and will the registered methode
Result
With only 3 files and a little bit of code you have a fully functional comet application.
Current known ‘issues’
- Dont work behind a http proxy (we are working on it!)
- You need a flash browser plugin
Experiments
- We created also a Xbox XNE client (thanks to Hoax)
- We also plan to create a iphone app and a adobe air application
No Comments »
No comments yet.