<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface {
public function onOpen(ConnectionInterface $conn) {
}
public function onMessage(ConnectionInterface $from, $msg) {
}
public function onClose(ConnectionInterface $conn) {
}
public function onError(ConnectionInterface $conn, \Exception $e) {
}
}
PS C:\xampp\htdocs\MyChat> php bin/chat-server.php
PHP Fatal error: Uncaught Error: Class 'MyApp\Chat' not found in C:\xampp\htdocs\MyChat\bin\chat-server.php:8
Stack trace:
#0 {main}
thrown in C:\xampp\htdocs\MyChat\bin\chat-server.php on line 8
Fatal error: Uncaught Error: Class 'MyApp\Chat' not found in C:\xampp\htdocs\MyChat\bin\chat-server.php:8
Stack trace:
#0 {main}
thrown in C:\xampp\htdocs\MyChat\bin\chat-server.php on line 8
PS C:\xampp\htdocs\MyChat>
<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
// Store the new connection to send messages to later
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from, $msg) {
$numRecv = count($this->clients) - 1;
echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
, $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
foreach ($this->clients as $client) {
if ($from !== $client) {
// The sender is not the receiver, send to each client connected
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn) {
// The connection is closed, remove it, as we can no longer send it messages
$this->clients->detach($conn);
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
require dirname(__DIR__) . '/vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8080
);
$server->run();
// define callbacks for this type and make it a unique type
// as we don't want two panels sharing the same global data.
scriptedPanelType
-ccb sampleCreateCallback
-icb sampleInitCallback
-acb sampleAddCallback
-rcb sampleRemoveCallback
-dcb sampleDeleteCallback
-scb sampleSaveStateCallback
-unique true
"sampleScriptedPanelType6";
global proc sampleCreateCallback(string $panelName) {
//
// Description:
// Create any editors unparented here and do
// any other initialization required.
//
// In this example we will only declare a global array to
// maintain some state information.
//
global float $gSampleState[5];
}
global proc sampleInitCallback(string $panelName) {
//
// Description:
// Re-initialize the panel on file -new or file -open.
//
// In this example we will only re-init the global array.
//
global float $gSampleState[];
$gSampleState[0] = 20.2;
$gSampleState[1] = 50.5;
$gSampleState[2] = 34.7;
$gSampleState[3] = 2.0;
$gSampleState[4] = 1.0;
}
global proc sampleAddCallback(string $panelName) {
//
// Description: Create UI and parent any editors.
//
global float $gSampleState[];
columnLayout -adj true topCol;
separator -style "none" -h 10;
frameLayout -l "Sliders" -mw 10;
columnLayout -adj true sampleCol;
separator -style "none" -h 10;
floatSliderGrp -l "Property A" -f true
-v $gSampleState[0]
fsg1;
floatSliderGrp -l "Property B" -f true
-v $gSampleState[1]
fsg2;
floatSliderGrp -l "Property C" -f true
-v $gSampleState[2]
fsg3;
separator -style "none" -h 10;
setParent ..;
setParent ..;
separator -style "none" -h 10;
frameLayout -l "Radio Buttons" -mw 10;
columnLayout sampleCol2;
separator -style "none" -h 10;
radioButtonGrp -nrb 3
-l "Big Options"
-la3 "Option 1" "Option 2" "Option 3"
-select $gSampleState[3]
rbg;
radioButtonGrp -nrb 3
-l "Little Options"
-la3 "Option 4" "Option 5" "Option 6"
-select $gSampleState[4]
rbg2;
separator -style "none" -h 10;
}
global proc sampleRemoveCallback(string $panelName) {
//
// Description:
// Unparent any editors and save state if required.
//
global float $gSampleState[];
// Scope the control names to this panel.
//
string $control = `scriptedPanel -q -control $panelName`;
setParent $control;
$gSampleState[0] = `floatSliderGrp -q -v fsg1`;
$gSampleState[1] = `floatSliderGrp -q -v fsg2`;
$gSampleState[2] = `floatSliderGrp -q -v fsg3`;
$gSampleState[3] = `radioButtonGrp -q -sl rbg`;
$gSampleState[4] = `radioButtonGrp -q -sl rbg2`;
}
global proc sampleDeleteCallback(string $panelName) {
//
// Description:
// Delete any editors and do any other cleanup required.
}
global proc string sampleSaveStateCallback(string $panelName) {
//
// Description:
// Return a string that will restore the current state
// when it is executed.
global float $gSampleState[];
$indent = "\n\t\t\t";
return ($indent+"$gSampleState[0]="+$gSampleState[0]+";" +
$indent+"$gSampleState[1]="+$gSampleState[1]+";" +
$indent+"$gSampleState[2]="+$gSampleState[2]+";" +
$indent+"$gSampleState[3]="+$gSampleState[3]+";" +
$indent+"$gSampleState[4]="+$gSampleState[4]+";" +
$indent+"setSamplePanelState $panelName;\n" );
}
global proc setSamplePanelState( string $whichPanel ) {
//
// Description:
// This is a convenience proc to set the panel state from the
// global array
global float $gSampleState[];
// Scope the control names to this panel.
//
string $control = `scriptedPanel -q -control $whichPanel`;
if ("" != $control) {
setParent $control;
floatSliderGrp -e -v $gSampleState[0] fsg1;
floatSliderGrp -e -v $gSampleState[1] fsg2;
floatSliderGrp -e -v $gSampleState[2] fsg3;
if (0 != $gSampleState[3]) {
radioButtonGrp -e -sl $gSampleState[3] rbg;
};
if (0 != $gSampleState[4]) {
radioButtonGrp -e -sl $gSampleState[4] rbg2;
}
}
}
// This script will create an unparented scripted panel, place it
// in one window, remove it, and place it in another window then
// return it to the first window.
//
// Create unparented scripted panel
//
scriptedPanel -unParent -type "sampleScriptedPanelType6" -label "Sample" "sampleScriptedPanel6";
// Create a couple of windows and parent the scripted panel to the first.
//
window "sampleWin6";
frameLayout -lv false -bv false frm;
scriptedPanel -e -parent "sampleWin6|frm" "sampleScriptedPanel6";
showWindow;
/*
window -w `window -q -w sampleWin` -h `window -q -h "sampleWin"` "sampleWin2";
frameLayout -lv false -bv false frm;
showWindow;
*/