WebSocket Protocol
Currently, the WebSocket protocol version of Workerman is 13.
The WebSocket protocol is a new protocol in HTML5. It enables full-duplex communication between the browser and the server.
Relationship Between WebSocket and TCP
Like HTTP, WebSocket is an application layer protocol, and both are based on TCP transmission. WebSocket itself is not closely related to Socket and should not be equated.
WebSocket Protocol Handshake
The WebSocket protocol has a handshake process, during which the browser and server communicate using the HTTP protocol. In Workerman, you can intervene in the handshake process as follows.
When workerman <= 4.1
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Connection\TcpConnection;
use Workerman\Worker;
$ws = new Worker('websocket://0.0.0.0:8181');
$ws->onConnect = function($connection)
{
$connection->onWebSocketConnect = function($connection , $httpBuffer)
{
// You can check if the connection source is valid here; if not, close the connection
// $_SERVER['HTTP_ORIGIN'] indicates which site's page initiated the websocket connection
if($_SERVER['HTTP_ORIGIN'] != 'https://www.workerman.net')
{
$connection->close();
}
// In onWebSocketConnect, $_GET and $_SERVER are available
// var_dump($_GET, $_SERVER);
};
};
Worker::runAll();
When workerman >= 5.0
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
use Workerman\Worker;
$worker = new Worker('websocket://0.0.0.0:12345');
$worker->onWebSocketConnect = function (TcpConnection $connection, Request $request) {
if ($request->header('origin') != 'https://www.workerman.net') {
$connection->close();
}
var_dump($request->get());
var_dump($request->header());
};
Worker::runAll();
WebSocket Protocol Transmission of Binary Data
The WebSocket protocol defaults to transmitting utf8 text. If you need to transmit binary data, please read the following section.
In the WebSocket protocol header, a flag is used to indicate whether the data being transmitted is binary data or utf8 text data. The browser will verify whether the flag and the content type being transmitted match; if they do not match, an error will occur, and the connection will be terminated.
Therefore, when the server sends data, it needs to set this flag based on the type of data being transmitted. In Workerman, if it is ordinary utf8 text, it needs to be set (this is the default value and usually does not need to be set manually)
use Workerman\Protocols\Websocket;
$connection->websocketType = Websocket::BINARY_TYPE_BLOB;
If it is binary data, it needs to be set to
use Workerman\Protocols\Websocket;
$connection->websocketType = Websocket::BINARY_TYPE_ARRAYBUFFER;
Note: If $connection->websocketType is not set, it defaults to BINARY_TYPE_BLOB (which is the utf8 text type). Generally, the application transmits utf8 text, such as transmitting json data, so there's no need to manually set $connection->websocketType. This property should only be set to BINARY_TYPE_ARRAYBUFFER when transmitting binary data (for example, image data, protobuffer data, etc.).
Using Workerman as a WebSocket Client
You can use the AsyncTcpConnection class in conjunction with the ws protocol to make Workerman connect to a remote WebSocket server as a WebSocket client, achieving bidirectional real-time communication.