laravel-swoole icon indicating copy to clipboard operation
laravel-swoole copied to clipboard

Target class [swoole.websocket] does not exist.

Open nifengfanpan opened this issue 4 years ago • 15 comments

Make sure you read Issues Guideline and answer these questions before submitting your issue. Thanks! (Any non-English issues will be closed immediately.)

  1. Please provide your PHP and Swoole version. (php -v and php --ri swoole) PHP Version | 7.3.11 Swoole Version | 4.4.17
  2. Please provide your Laravel/Lumen version. Laravel Version | 7.5.2
  3. Which release version of this package are you using? ^2.6
  4. What did you do? If possible, provide a recipe for reproducing the error
use SwooleTW\Http\Websocket\Facades\Websocket;

Websocket::toUserId(1)->emit('message','test');
  1. What did you expect to see? I expected the websocket to emit the data to my client

But it is normal in websocket handler

  1. What did you see instead? Illuminate\Contracts\Container\BindingResolutionException Target class [swoole.websocket] does not exist.

nifengfanpan avatar May 22 '20 18:05 nifengfanpan

Added Package Service Providers:

app.php SwooleTW\Http\LaravelServiceProvider::class

But it can't run please for help @albertcht

nifengfanpan avatar May 31 '20 19:05 nifengfanpan

pecl compile without websocket support, reinstall swoole with support websockets

mxp100 avatar Jul 08 '20 09:07 mxp100

pecl compile without websocket support, reinstall swoole with support websockets

Source code install But it can't

nifengfanpan avatar Jul 21 '20 16:07 nifengfanpan

Same issue here.

pecl recompile / reinstall with all options YES, but still not working.

running the code below, works fine, but not with swooletw, why?

<?php

use Swoole\WebSocket\Server;
use Swoole\Http\Request;
use Swoole\WebSocket\Frame;

$server = new Server("0.0.0.0", 9502);

$server->on("start", function (Server $server) {
    echo "Swoole WebSocket Server is started at http://127.0.0.1:9502\n";
});

$server->on('open', function(Server $server, Swoole\Http\Request $request) {
    echo "connection open: {$request->fd}\n";
    $server->tick(1000, function() use ($server, $request) {
        $server->push($request->fd, json_encode(["hello", time()]));
    });
});

$server->on('message', function(Server $server, Frame $frame) {
    echo "received message: {$frame->data}\n";
    $server->push($frame->fd, json_encode(["hello", time()]));
});

$server->on('close', function(Server $server, int $fd) {
    echo "connection close: {$fd}\n";
});

$server->start();

adrianowead avatar Sep 30 '20 12:09 adrianowead

How did you run it @adrianowead ?

Arkanius avatar Oct 21 '20 02:10 Arkanius

How did you run it @adrianowead ?

The code above are available on https://www.swoole.co.uk/docs/modules/swoole-websocket-server

To run, justo save file and execute on bash:

php websocket.php

adrianowead avatar Nov 01 '20 17:11 adrianowead

I also encountered the same problem.

The operating environment is as follows: PHP Version | 7.2.34 Swoole Version | 4.4.17 Laravel Version | 6.20.27 This package Version | "^2.8"

Error message:

Illuminate\Contracts\Container\BindingResolutionException : Target class [swoole.websocket] does not exist.

at /usr/local/var/www/laravel_im/vendor/laravel/framework/src/Illuminate/Container/Container.php:805 801| 802| try { 803| $reflector = new ReflectionClass($concrete); 804| } catch (ReflectionException $e) {

805| throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e); 806| } 807| 808| // If the type is not instantiable, the developer is attempting to resolve 809| // an abstract type such as an Interface or Abstract Class and there is

Exception trace:

1 ReflectionException::("Class swoole.websocket does not exist") /usr/local/var/www/laravel_im/vendor/laravel/framework/src/Illuminate/Container/Container.php:803

2 ReflectionClass::__construct("swoole.websocket") /usr/local/var/www/laravel_im/vendor/laravel/framework/src/Illuminate/Container/Container.php:803

ly0216 avatar Jun 15 '21 08:06 ly0216

@nifengfanpan Is this problem finally solved?

ly0216 avatar Jun 15 '21 08:06 ly0216

@nifengfanpan 这个问题终于解决了?

大哥你解决了嘛,我PHP8.1 SWOOLE4.8.4报了一样的错误

maonini avatar Dec 20 '21 06:12 maonini

你看下是不是在启动了swoole的环境下运行的,我使用的是php7.3 + swoole4.+版本 原因是我在web环境调用的接口造成失败,实际上是需要在启动了swoole的环境使用,比如我安装的虚拟机,虚拟机里安装swoole,在虚拟机里操作就没有问题了

------------------ 原始邮件 ------------------ 发件人: @.>; 发送时间: 2021年12月20日(星期一) 下午2:45 收件人: @.>; 抄送: @.>; @.>; 主题: Re: [swooletw/laravel-swoole] Target class [swoole.websocket] does not exist. (#415)

@nifengfanpan 这个问题终于解决了?

大哥你解决了嘛,我PHP8.1 SWOOLE4.8.4报了一样的错误

— Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you commented.Message ID: @.***>

lutyne avatar Dec 20 '21 06:12 lutyne

你看下是不是在启动了swoole的环境下运行的,我用的是php7.3+swoole4.+版本原因是我在web环境调用的接口导致失败,实际上是需要在启动了swoole的环境使用,比如我安装的虚拟机,虚拟机里安装swoole,在虚拟机里操作就没有问题了 ------------------ 原始邮件 ------------------ 发件人: @.>; 发送时间: 2021年12月20日(星期一) 下午2:45 收件人: @.>; 抄送: @.>; @.>; 主题: Re: [swooletw/laravel-swoole] Target class [swoole.websocket] does not exist. (#415) @nifengfanpan 这个问题终于解决了? 大哥你解决了嘛,我PHP8.1 SWOOLE4.8.4报了一样的错误 — Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you commented.Message ID: @.***>

我也是在虚拟机上运行的,肯定是在swoole的环境下,因为我注释掉websocket路由可以正常启动

maonini avatar Dec 20 '21 07:12 maonini

嗯嗯,我的意思是即使调用接口也要在虚拟机环境下,如果这样也不行的话那你和我的问题就不一样了,可能是PHP8的原因,可能是其他原因,或者你尝试下降到7.3版本运行试试

------------------ 原始邮件 ------------------ 发件人: @.>; 发送时间: 2021年12月20日(星期一) 下午3:03 收件人: @.>; 抄送: @.>; @.>; 主题: Re: [swooletw/laravel-swoole] Target class [swoole.websocket] does not exist. (#415)

你看下是不是在启动了swoole的环境下运行的,我用的是php7.3+swoole4.+版本原因是我在web环境调用的接口导致失败,实际上是需要在启动了swoole的环境使用,比如我安装的虚拟机,虚拟机里安装swoole,在虚拟机里操作就没有问题了 … ------------------ 原始邮件 ------------------ 发件人: @.>; 发送时间: 2021年12月20日(星期一) 下午2:45 收件人: @.>; 抄送: @.>; @.>; 主题: Re: [swooletw/laravel-swoole] Target class [swoole.websocket] does not exist. (#415) @nifengfanpan 这个问题终于解决了? 大哥你解决了嘛,我PHP8.1 SWOOLE4.8.4报了一样的错误 — Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you commented.Message ID: @.***>

我也是在虚拟机上运行的,肯定是在swoole的环境下,因为我注释掉websocket路由可以正常启动

— Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you commented.Message ID: @.***>

lutyne avatar Dec 20 '21 07:12 lutyne

The same issue. I can't resolve.

musichook avatar Aug 01 '23 10:08 musichook

Someone solved this problem? PHP Version | 8.1.27 Swoole Version | 5.1.1 Laravel Version | 10.46.0

Rakashikov avatar Mar 04 '24 13:03 Rakashikov

@Rakashikov, If your setup includes PHP-FPM, it's important to note that it will not function alongside Swoole. To succinctly put it, you must employ the Swoole HTTP server to enable the use of the [swoole.websocket] class.


PHP-FPM and Swoole are both process managers designed with distinct functionalities in mind and are not intended to work in conjunction. Specifically, the Websocket server is initialized solely through the swoole:http start command, making it accessible only to HTTP requests managed by the Swoole server. This setup enables Swoole to handle all HTTP requests, leveraging its benefits, such as keeping the application loaded in memory for quicker responses. Consequently, PHP-FPM becomes unnecessary in this context, allowing swoole:http to serve as the primary process in the PHP container.

Upon examining the package code, it's evident that Websocket initialization occurs within Concerns\InteractsWithWebsocket, a component exclusively utilized by Server\Manager. This particular instance is generated solely through Commands/HttpServerCommand. Attempting to instantiate it within a PHP-FPM environment results in an exception, underscoring the incompatibility between PHP-FPM and Swoole in this scenario.

musichook avatar Apr 04 '24 21:04 musichook