Skip to content
This repository has been archived by the owner on Oct 19, 2020. It is now read-only.

How to use

William Mcmurray edited this page Oct 20, 2013 · 9 revisions

Create a server

  1. Create a new class extending SocketServer and place it in "/servers/MyServer/"
  2. Overwrite method called "init()" in your extended class
  3. Instanciate this class and call method "run()"
  4. In dir "/executables/YOUR_OPERATING_SYSTEM/", create an executable file to execute your script
  5. Execute this executable file and voilà !

Exemple : extended class file "/servers/MyServer/MyServer.php" :

<?php
require_once("../../core/index.php");
class MyServer_SocketServer extends SocketServer
{
	protected function init()
	{
		//...
	}
}

$server = new MyServer_SocketServer("127.0.0.1",8080);
$server->run();
?>

Exemple : windows executable file "/executables/windows/MyServer.bat" :

php ../../core/wrapper.php -server:MyServer -verbose

Exemple : linux executable file "/executables/linux/MyServer.sh" :

#!/bin/bash
php ../../core/wrapper.php -server:MyServer -verbose

Handle data received by server

Each time the server receive data from a client, it'll try to call a method to handle it so you have to declare one in your extended class for every actions you want your server to handle. Function name have to be like this :

handle_(ACTION_NAME_HERE)($client, $data);

Exemple : a server which can handle "helloworld" action :

<?php
require_once("../../core/index.php");
class HelloWorld_SocketServer extends SocketServer
{
	protected function init()
	{
		// set the name of the server (displayed in command prompt)
		$this->set_server_name("Hello World Server");
	}
	
	protected function handle_helloworld($client, $data)
	{
		// will simply output data received in command prompt
		output($data);
		
		// and send back data to every connected clients
		$this->send_to_all("helloworld", $data);
	}
}

$server = new HelloWorld_SocketServer("127.0.0.1", 8080);
$server->run();
?>

Run your server

Now it's time to start the beast ! You can create an executable file in the directory "/executable/YOUR_OPERATING_SYSTEM/" or you can execute it manually from a command prompt. In this case, keep in mind that every paths are relatives to the "/executable/YOUR_OPERATING_SYSTEM/" directory so you'll have to travel there in command prompt before starting the server.

The server is made in PHP so you'll have to run your server through PHP like this :

php ../../core/wrapper.php -server:MyServer -verbose

If you get an error message like "PHP is not recognized as an internal command... bla bla bla" you'll need to add the path to the binary in your system OR simply replace "php" by the path to the php.exe you have on your computer like this :

C:/wamp/bin/php/php5.2.9-2/php.exe ../../core/wrapper.php -server:MyServer -verbose

Your server script will run inside of the "wrapper.php" script to enable server reboot capability and such. You tell this wrapper which script it should run by changing the second part of the special option "-server:". Just write the name of your server's PHP file after it (without space).

You may have also notice the "-verbose" option, there is some more options you can pass to your script that will affect it's behaviour, a complete list of them may be found here.

Create a client

Now that we have a working server, it's time to have a client ! In the directory "/public/lib/" you'll find a file named "SocketClient.js", this is the JavaScript client you gonna use. Because JavaScript is not like PHP in terms of class extending and such, I suggest you create a new class with a a property containing a client instance like this :

function MyClient(address, port)
{
	this.init = function()
	{
		// instanciate the SocketClient class
		this.socket = new SocketClient(address, port);

		// eventListener executed when socket is opened
		this.socket.on("open", function(){ alert("Socket opened."); });

		// eventListener executed when socket is closed
		this.socket.on("close", function(e){ alert("Socket closed with code : " + e.code); });

		// eventListener executed when an error occurs
		this.socket.on("error", function(e){ alert("An error occured."); });
		
		// eventListener executed when receiving a message from server
		this.socket.on("message", this.handle_messages);

		// opening of the socket
		this.socket.open();
	}

	// this method handle all incoming data from the server
	this.handle_messages = function(data)
	{
		switch(data.action)
		{
			// when data is received from server with action "helloworld"
			case "helloworld" :
				alert("Data received : " + data.content);
			break;
		}
	}
	
	// initialization
	this.init();
}

Run your client

Your client is all set to communicate with the server. Now we need to instantiate it like this :

var client = new MyClient("127.0.0.1", "8080");

by passing the IP address and port on which the server listen. Then send data to server like this :

client.send("helloworld", "Hello World !!");

That's it young padawan ! If everything has been set properly, the server should receive data and handle it. :)

Clone this wiki locally