PHP code example of huaweichenai / web-socket
1. Go to this page and download the library: Download huaweichenai/web-socket 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/ */
huaweichenai / web-socket example snippets
$host = '0.0.0.0';
$port = '8888';
$configs = [
// 日志文件路径
'log_file' => dirname(__DIR__) . '/logs/swoole.log',
// 进程的PID存储文件
'pid_file' => dirname(__DIR__) . '/logs/swoole.server.pid'
]
class Swoole extends WebSocketServer
{
/**
* @var bool
* 开启自定义握手处理
*/
public $handshake = true;
/**
* @param \swoole_websocket_server $server
*
* 自定义websocket服务启动处理
*/
public function socketStart($server)
{
//业务代码
}
/**
* @param \swoole_websocket_server $server
* @param \swoole_http_request $request
*
* 自定义websocket建立连接处理
*/
public function socketOpen($server, $request)
{
//业务代码
}
/**
* @param \swoole_websocket_server $server
* @param \swoole_websocket_frame $frame
*
* 自定义websocket 接受客户端消息处理
*/
public function socketMessage($server, $frame)
{
//业务代码
}
/**
* @param \swoole_http_request $request
* @param \swoole_http_response $response
*
* 自定义websocket握手处理
*/
public function socketHandshake($request, $response)
{
//业务代码
//业务代码
}
/**
* @param \swoole_http_request $request
* @param \swoole_http_response $response
*
* 自定义websocket http响应处理
*/
public function socketRequest($request, $response)
{
//业务代码
}
/**
* @param \swoole_websocket_server $server
* @param $fd
*
* 自定义websocket 客户端连接关闭处理
*/
public function socketClose($server, $fd)
{
//业务代码
}
/**
* @param \swoole_websocket_server $server
*
* 自定义websocket 服务端正常关闭处理
*/
public function socketShutdown($server)
{
//业务代码
}
}
$this->sendMessage($request->fd, $server,$data);
$this->getParams($request) //获取客户端连接路由
$this->getRoute($request) //获取客户端传参