PHP Fatal error: swoole_http_server::start(): require onMessage callback

来源:4-10 Swoole task任务使用(上)

紫川虚伪

2018-04-05

使用的是php 7.0.21 环境  在将swoole_websocket_server 面向过程 写为类 启动 出现报错  

 PHP Fatal error:  swoole_http_server::start(): require onMessage callback in /home/wwwroot/default/demo/server/ws.php on line 17

<?php


class Ws
{
CONST HTOS = "0.0.0.0";
CONST POST = 8812;
public $ws = null;
public function __construct()
{
$this->ws = new swoole_websocket_server("0.0.0.0", 8812);
$this->ws->on("open", [$this,'onOpen']);
$this->ws->on("open", [$this,'onMessage']);
$this->ws->on("open", [$this,'onClose']);

$this->ws->start();
}
/**
     * 监听ws链接事件
     * @DateTime  2018-04-05
     * @copyright [copyright]
     * @license   [license]
     * @version   [version]
     * @param     [$ws]
     * @param     [$request]
     * @return    [type]
     */
public function onOpen($ws,$request){
var_dump($request->fd);
}
/**
     * 监听ws消息事件 收到客户端的消息
     * @DateTime  2018-04-05
     * @copyright [copyright]
     * @license   [license]
     * @version   [version]
     * @param     [type]
     * @param     [type]
     * @return    [type]
     */
public function onMessage($ws,$frame){
echo "收到了你的消息".$frame->data;
$ws->push($frame->fd, 'server-push:'.date("Y-m-d H:i:s"));
}
/**
     * 关闭
     * @DateTime  2018-04-05
     * @copyright [copyright]
     * @license   [license]
     * @version   [version]
     * @param     [type]
     * @param     [type]
     * @return    [type]
     */
public function onClose($ws,$fd){
echo "关闭客户端id".$fd;
}
}


$obj = new Ws();


写回答

1回答

singwa

2018-04-05

$this->ws->on("open", [$this,'onMessage']);
$this->ws->on("open", [$this,'onClose']);

需要修改为下面的代码:

$this->ws->on("message", [$this,'onMessage']);
$this->ws->on("close", [$this,'onClose']);


并且在您类中还需要加一个 onClose方法

0
1
紫川虚伪
非常感谢!
2018-04-07
共1条回复

Swoole入门到实战打造高性能赛事直播平台

swoole创始人推荐,借助Swoole打造高性能直播平台

2228 学习 · 1062 问题

查看课程