PHP code example of listen / websocket-client
1. Go to this page and download the library: Download listen/websocket-client 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/ */
listen / websocket-client example snippets
try {
\WebSocketClient::connect();
\WebSocketClient::send("hello hahaha\n");
echo \WebSocketClient::recv() . "hahaha\n";
} catch (\Listen\Swoole\Exceptions\WebSocketException $e) {
dd($e->getMessage());
}
try {
// 实例化 websocket 客户端
$client = new WebSocket($host, $port);
// 连接服务器
$result = $client->connect();
// 发送消息
$client->send("Hello Word \n");
// 输出服务消息
echo $client->recv() . "\n";
} catch (WebSocketException $e) {
echo $e->getMessage();
}