这个文件运行我的服务器
<?php
class websocket
{
public $ws;
public function start()
{
$this->ws = new swoole_websocket_server('127.0.0.1', 9502);
$this->ws->on('open', function ($ws, $request) {
echo "connection open: {$request->fd}\n";
});
$this->ws->on('message', function ($ws, $frame) {
echo "received message: {$frame->data}\n";
$this->ws->push($frame->fd, json_encode(["hello", "world"]));
});
$this->ws->on('close', function ($ws, $id) {
$this->onClose($id);
});
$this->ws->start();
$this->sendMessage(1, "asdfasdf");
}
public function sendMessage($id,$msg)
{
$this->ws->push($id, "asdf");
}
}
我像这样从 cli 运行它:
php -r 'include("websocket.php"); $web = new websocket; $web->start();'
然后我在浏览器上打开这个文件
<?php
include ('websocket.php');
$n = new websocket();
$n->ws->push(1, "asdf", 1, true);
我得到这个错误:
127.0.0.1:51180 [500]:GET /send.php - 未捕获的错误:在 /home/ganbatte/Desktop/123/send.php:4 中的 null 上调用成员函数 push()
为什么会这样,我该如何解决?