What exactly is this?
Thalassa is a lightweight implementation of the WAMP V2 protocol, written in php.
!What is
Thalassa is not a web-socket server.Although it is built upon web-sockets and fully implements the web-socket protocol, Thalassa will only work for clients implementing WAMP as a websocket sub-protocol, and hence is better defined as a WAMP server, as opposed to a WAMP-capable websocket server.
Features
Thalassa features an event-driven IO using libevent, and implements the role of a broker and a dealer.
Requirements
A working installation of the libevent library and necessary extensions.
Installing
Composer:
"require": {"nsautomaton/thalassa": "dev-master"}
new to composer? See installation and basic usage.
Hello world!
tag
namespace wampComponent;
use Thalassa\wampAppInterface;
use Thalassa\Dealer\proceduresInterface;
use Thalassa\Broker\channelsInterface;
class wampApp implements wampAppInterface{
public function onOpen($conn, $sessionKey){}
public function onSubscribe(channelsInterface $channelAccess, $conn, $requestID, $channel)
{
$channelAccess->add($conn, $channel, $requestID);
}
public function onUnsubscribe(channelsInterface $channelAccess, $conn, $channel, $requestID)
{
$channelAccess->delete($conn, $channel, $requestID);
}
public function onPublish(channelsInterface $channelAccess, $conn, array $data, array $options)
{
$channelAccess->broadcast($channelAccess, $conn, $data, $options);
}
public function onRegister(proceduresInterface $exe, $conn, $call, $requestID, array $options){}
public function onUnregister(proceduresInterface $exe, $conn, $regID, $requestID){}
public function onCall(proceduresInterface $exe, $conn, $call, array $args, array $options){}
public function onError($conn, $details){}
public function onClose($conn){}
}
The snippet is a bare minimum of a wamp server.all it does is handle subscriptions and route messages between subscribers.
In your project directory, create a folder "src", if it doesn't exist yet.
In "src", create a folder "wampComponent."
Save the code as "wampApp.php". Alternatively, you can save it as whatever_name_you_like, provided the class name matches the file name.This is because we're using psr-0 autoloading. Similarly, the folder "wampComponent" could be any name you like, as long as it matches the namespace of the code that resides in it.
In your composer.json:
"autoload": {
"psr-0": {
"wampComponent": "src"
}
}
Update composer by re-running install to regenerate vendor/autoload.php:
composer install
or
composer update
...and now the shell script:
tag
use Thalassa\Server;
use wampComponent\wampApp;
require dirname(__DIR__) . '/vendor/autoload.php';
$host = '0.0.0.0';
$port = 9000;
$app = new wampApp;
$server = new Server($host, $port);
$server->Build($app);
$server->start();
Create a folder "bin" and save the code simply as daemon.php
That's all.To start the server simply cd your way to to /bin and type the command:
php daemon.php
But wait!What about the client-side code?
Thalassa could work with any WAMP V2 client.See a list of clients and other routers For this tutorial Autobahn|JS is used. Learn more and view the full Autobahn|JS documentation.
smlhtml_ml
smlhead_ml
smlmeta charset='utf-8'_ml
smlscript type="text/javascript" src="path_to_your_autobahn|js_copy"_mlemlscript_ml
smlscript type="text/javascript"_ml
function onevent(message)
{
console.log(message);
};
function send()
{
sess.publish("com.test.app", "Hello World", {exclude_me:false});
};
window.onload = function(){
var connection = new autobahn.Connection({url : 'ws://127.0.0.1:9000'});
connection.onopen = function(session){
window.sess = session;
session.subscribe("com.test.app", onevent).then(
function(subscription)
{
console.log("subscription successful");
},
function(error)
{
console.log("subscription error");
});
console.log('Connection established');
};
connection.onclose = function(session){
console.log('Connection lost');
};
connection.open();
};
emlscript_ml
emlhead_ml
smlbody_ml
smlinput type='button' value="send the message!" onclick='send();'zml
emlbody_ml
emlhtml_ml