Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5
<?php OpenSwoole\WebSocket\Server->on('Request', callable $callback)
The event name to set a callback for
Callable function for the server event type
If success, it returns true
, otherwise it returns false
The WebSocket server inherits the OpenSwoole\Http\Server
object and gains access to the HTTP server API and features. To learn more it is suggested to read the OpenSwoole HTTP Server
documentation.
The Request
event is usually part of the HTTP Server class but it can also be used within a WebSocket server. By setting the Request
event, OpenSwoole will trigger the callback to handle HTTP protocol requests. It is like running a WebSocket and HTTP server at the same time.
If you do not register the Request
event with a WebSocket server, any HTTP request will result in a 400
response given to the client.
You may also use the HTTP server configuration when running a WebSocket server.
You are not required to register this callback event
Even though we get access to the Request
event within a WebSocket server, sending data back to the client is a little different, so let's take a look at some examples.
Inside a normal server.php
file, we can start up a WebSocket server which responds to HTTP requests and broadcasts back to all clients who are connected.
<?php
$server = new OpenSwoole\WebSocket\Server("127.0.0.1", 9501);
$server->on('Open', function(OpenSwoole\WebSocket\Server $server, $request)
{
echo "server: handshake success with fd{$request->fd}\n";
});
$server->on('Message', function(OpenSwoole\WebSocket\Server $server, $frame)
{
echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
$server->push($frame->fd, "this is server");
});
$server->on('Close', function(OpenSwoole\WebSocket\Server $server, $fd)
{
echo "client {$fd} closed\n";
});
// The Request event closure callback is passed the context of $server
$server->on('Request', function(OpenSwoole\Http\Request $request, OpenSwoole\Http\Response $response) use ($server)
{
/*
* Loop through all the WebSocket connections to
* send back a response to all clients. Broadcast
* a message back to every WebSocket client.
*/
foreach($server->connections as $fd)
{
// Validate a correct WebSocket connection otherwise a push may fail
if($server->isEstablished($fd))
{
$server->push($fd, $request->get['message']);
}
}
});
$server->start();
In this example we can use a PHP object to store our web server and access the main $server
to send back data to the connected users, broadcasting back to all WebSocket clients.
<?php
class WebSocketServerTest
{
public $server;
public function __construct()
{
$this->server = new OpenSwoole\WebSocket\Server("127.0.0.1", 9501);
$this->server->on('Open', function(OpenSwoole\WebSocket\Server $server, $request)
{
echo "server: handshake success with fd{$request->fd}\n";
});
$this->server->on('Message', function(OpenSwoole\WebSocket\Server $server, $frame)
{
echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
$server->push($frame->fd, "this is server");
});
$this->server->on('Close', function(OpenSwoole\WebSocket\Server $server, $fd)
{
echo "client {$fd} closed\n";
});
// The Request event closure callback is passed the context of $server
$this->server->on('Request', function(OpenSwoole\Http\Request $request, OpenSwoole\Http\Response $response)
{
/*
* Loop through all the WebSocket connections to
* send back a response to all clients. Broadcast
* a message back to every WebSocket client.
*/
foreach($this->server->connections as $fd)
{
// Validate a correct WebSocket connection otherwise a push may fail
if($this->server->isEstablished($fd))
{
$this->server->push($fd, $request->get['message']);
}
}
});
}
public function start()
{
$this->server->start();
}
// ...
}
// Create a new WebSocket server instance
$webSocketServer = new WebSocketServerTest();
// Start the server
$webSocketServer->start();