如果使用php作为客户端 链接socket 本文借助了workerman来实现的,所以小伙伴们需要到workerman的官网下载
composer require workerman/workerman
下面就直接上代码了
<?php
require_once 'vendor/autoload.php';
use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;
$worker = new Worker();
$worker->onWorkerStart = function($worker){
$con = new AsyncTcpConnection('ws://xxxxxx');
global $db;
$db = new \Workerman\MySQL\Connection('127.0.0.1', '3306', 'x', 'x', 'x');
global $count;
$count = 0;
$con->onConnect = function($con) {
$send=json_encode([]);
$con->send($send);
};
$con->onMessage = function($con, $data) use ($db) {
};
$con->onClose = function ($con) {
$con->reConnect(1);
};
$con->onError = function ($connection, $code, $msg) {
echo "error $code $msg\n";
};
$con->connect();
};
Worker::runAll();
|