PHP code example of hemiframe / php-websocket
1. Go to this page and download the library: Download hemiframe/php-websocket library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hemiframe / php-websocket example snippets
$socket = new \HemiFrame\Lib\WebSocket\WebSocket("localhost", 8080);
$socket->on("receive", function($client, $data) use($socket) {
foreach ($socket->getClients() as $item) {
/* @var $item \HemiFrame\Lib\WebSocket\Client */
if ($item->id != $client->id) {
$socket->sendData($item, $data);
}
}
});
$socket->startServer();
$socket = new \HemiFrame\Lib\WebSocket\WebSocket('localhost', 8080);
$socket->on("receive", function($client, $data) use($socket) {
});
$client = $socket->connect();
if ($client) {
$socket->sendData($client, "Message");
$socket->disconnectClient($client);
}
$socket->on("connect", function(\HemiFrame\Lib\WebSocket\Client $client) {
});
$socket->on("receive", function(\HemiFrame\Lib\WebSocket\Client $client, $data) {
});
$socket->on("send", function(\HemiFrame\Lib\WebSocket\Client $client, $data) {
});
$socket->on("ping", function(\HemiFrame\Lib\WebSocket\Client $client, $data) {
});
$socket->on("pong", function(\HemiFrame\Lib\WebSocket\Client $client, $data) {
});
$socket->on("disconnect", function(\HemiFrame\Lib\WebSocket\Client $client, $statusCode, $reason) {
});
$socket->on("error", function($socket, $client, $phpError, $errorMessage, $errorCode) {
});
$socket->getSocket();
/**
* Get connected clients
* @return array
*/
$socket->getClients();
/**
* Get connected clients, filtered by path
* @return array
*/
$socket->getClientsByPath($path = "/");
$socket->getAllowedOrigins();
$socket->setAllowedOrigins($allowedOrigins);
$socket->getMaxClients();
$socket->setMaxClients($maxClients);
$socket->getBufferSize();
$socket->setBufferSize($bufferSize);
$socket->getUserAgent();
$socket->setUserAgent($userAgent);
$socket->setEnableLogging($enableLogging);
$socket->create();
$socket->close();
$socket->setOption($socket, $level, $optname, $optval);
$socket->bind($address, $port = 0);
$socket->listen($backlog = 0);
$socket->select(&$read, &$write, &$except, $backlog, $tv_usec = 0);
$socket->accept();
$socket->write($socket, $message = null);
$socket->recv($socket);
$socket->read($socket);
/**
* Get last socket error code
* Alias of socket_last_error
* @return int
*/
$socket->getLastErrorCode();
/**
* Get last socket error message
* @return string
*/
$socket->getLastErrorMessage();
/**
* Connect to websocket server. Send and validate handshake request.
* @param string $path Send custom path to server
* @param string $origin Send custom origin host to server
* @return mixed Return client object when is successfully connected to server or FALSE when is not.
*/
$socket->connect($path = "/", $origin = null);
/**
* Run WebSocket server.
* Create main socket.
* Bind address and port.
* Listens for a connection on a socket.
* Call main processing loop
*/
$socket->startServer();
/**
* Disconnect all clients and close main socket
*/
$socket->stopServer();
/**
* Main processing loop. Check for new clients, incomming data, diconnected clients
*/
$socket->loop();
/**
* Trigger event
* @param string $name Event name
* @param array $arguments Arguments for sending
*/
$socket->trigger($name, $arguments);
/**
* Subscribe for event.
* @param string $name Event name
* @param function $function Callback function
*/
$socket->on($name, $function);
/**
* Subscribe on event. Call only first time
* @param string $name Event name
* @param function $function Callback function
*/
$socket->one($name, $function);
/**
* Send data to client
* @param object $client
* @param string $data
*/
$socket->sendData($client, $data);
/**
* Disconnect client. You can send custom status code and reason
* @param object $client
* @param int $statusCode
* @param string $reason
* @return self
*/
$socket->disconnectClient($client, $statusCode = self::STATUS_CLOSE_NORMAL, $reason = null);
json
{
"emiframe/php-websocket": "~1.7"
}
}
php demo/server.php