為何還會出現如下notice資訊呢?
PHP Notice: Undefined property: swoole_http_request::$get in /var/local/code/swoole/ws_server.php on line 8
PHP Stack trace:
PHP 1. {main}() /var/local//swoole /ws_server.php:0
PHP 2. swoole_http_server->start() /var/local/code/swoole/ws_server.php:23
PHP 3. {closure:/var/local/code/swoole/ws_server.php: 7-10}() /var/local/code/swoole/ws_server.php:23
//创建websocket服务器对象,监听0.0.0.0:9502端口
$ws = new swoole_websocket_server("0.0.0.0", 9502);
//监听WebSocket连接打开事件
$ws->on('open', function ($ws, $request) {
var_dump($request->fd, $request->get, $request->server);
$ws->push($request->fd, "hello, welcome\n");
});
//监听WebSocket消息事件
$ws->on('message', function ($ws, $frame) {
echo "Message: {$frame->data}\n";
$ws->push($frame->fd, "server: {$frame->data}");
});
//监听WebSocket连接关闭事件
$ws->on('close', function ($ws, $fd) {
echo "client-{$fd} is closed\n";
});
$ws->start();
js程式碼
var wsServer = 'ws://127.0.0.1:9502';
var websocket = new WebSocket(wsServer);
websocket.onopen = function (evt) {
console.log("Connected to WebSocket server.");
};
websocket.onclose = function (evt) {
console.log("Disconnected");
};
websocket.onmessage = function (evt) {
console.log('Retrieved data from server: ' + evt.data);
};
websocket.onerror = function (evt, e) {
console.log('Error occured: ' + evt.data);
};
ws_server.php第六行,request物件沒有get方法
第一個請求是
favicon.ico
,这里没有get
這個屬性的。你把這個請求過濾掉就好了。