workerman例子

1.服务端

require_once 'vendor/autoload.php';
use Workerman\Worker;
// 创建一个Worker监听2345端口,使用http协议通讯
$ws_worker = new Worker("websocket://0.0.0.0:2345");
// 启动4个进程对外提供服务
$ws_worker->count = 4;
// 当收到客户端发来的数据后返回hello $data给客户端
$ws_worker->onMessage = function($connection, $data){
     foreach($connection->worker->connections as $con) {
        $con->send('你好');
    }
    //$connection->send($data);
};
// 运行worker
Worker::runAll();

2.客户端

 
 
 
HTML5 
 
 




如果需向连接的客户端发送消息


foreach($connection->worker->connections as $con) {
        $con->send('你好');
    }


向已连接的所有客户端进行发送消息

你可能感兴趣的:(workerman)