php 实现websockets客户端

耗尽温柔 提交于 2020-02-29 19:44:15

php 实现websockets客户端,需要利用swoole(参考文档:https://wiki.swoole.com/#/

require "WebSocketClient.php";

$client = new WebSocketClient();
$client->connect("127.0.01", 9501);
$client->on("open", function ($client) {
    $fd = $client->getTcpClient()->sock;
    echo "fd: $fd is open\n";
    $msg = ['data' =>"发送消息" ];
    $client->send(json_encode($msg));
});

$client->on("message", function ($client, $frame) {
    $fd = $client->getTcpClient()->sock;
    echo "fd: $fd received: {$frame->data}\n";
});

$client->on("close", function ($client) {
    $fd = $client->getTcpClient()->sock;
    echo "fd: $fd is closed\n";
    return;
});

$client->onClose($client);

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!