Laravel回显服务器未显示任何已连接的客户端?

时间:2018-10-05 08:37:55

标签: node.js socket.io laravel-echo

Laravel回显服务器正在开发环境中工作,但不适用于生产环境...

我打开了应该接收从laravel echo服务器广播的事件的网页。因此,浏览器控制台未显示任何错误,例如can't connect to ws://domain.com/socket.io/etc。服务器端什么也没有,我看不到任何客户端连接...

L A R A V E L  E C H O  S E R V E R

version 1.4.2

Starting server...

✔  Running at 0.0.0.0 on port 6001
✔  Listening for http events...
✔  Listening for redis events...

Server ready!

我希望看到类似的东西:

0|Socket-Connection  | [11:17:01 AM] - ********** authenticated for: private-user.1
0|Socket-Connection  | [11:17:01 AM] - ********** joined channel: private-user.1

这是在我的resources/assets/js/bootstrap.js中:

import Echo from "laravel-echo"
window.io = require('socket.io-client');

if (typeof io !== 'undefined') {
  window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001',
    transports: ['websocket', 'polling', 'flashsocket']
  });
}

会发生什么?

2 个答案:

答案 0 :(得分:0)

if (typeof window.io !== 'undefined') {
  window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001',
    transports: ['websocket', 'polling', 'flashsocket']
  });

答案 1 :(得分:0)

现在正在工作!我不知道该怎么做才能使它正常工作,也许是与Laravel Queue或Redis有关的问题...

Laravel回显服务器

这是对我有用的laravel-echo-server.json

{
        "authHost": "http://11.222.333.44",
        "authEndpoint": "/broadcasting/auth",
        "clients": [],
        "database": "redis",
        "databaseConfig": {
                "redis": {
                        "host": "127.0.0.1",
                        "port": "6379"
                },
                "sqlite": {
                        "databasePath": "/database/laravel-echo-server.sqlite"
                }
        },
        "devMode": false,
        "host": null,
        "port": "6001",
        "protocol": "http",
        "socketio": {},
        "sslCertPath": "",
        "sslKeyPath": "",
        "sslCertChainPath": "",
        "sslPassphrase": "",
        "subscribers": {
                "http": true,
                "redis": true
        },
        "apiOriginAllow": {
                "allowCors": true,
                "allowOrigin": "*",
                "allowMethods": "GET, POST",
                "allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
        }
}

注意:http://11.222.333.44是网站地址。

Laravel队列

可以肯定确定的另一件事是:queue:work进程是从Supervisor的错误队列连接(SQS)开始的,就像在Laravel Docs中一样:

command=php /home/forge/app.com/artisan queue:work sqs --sleep=3 --tries=3

我不使用Amazon SQS作为队列连接,而是默认使用Redis(在QUEUE_DRIVER中指定为.env),这就是它生成此错误的原因:

[2018-10-03 09:19:50] prod.ERROR: Error executing "ReceiveMessage" on "https://sqs.us-east-1.amazonaws.com/your-account-id/your-queue-name"; AWS HTTP error: Client error: `POST https://sqs.us-east-1.amazonaws.com/your-account-id/your-queue-name` resulted in a `403 Forbidden` response:
<?xml version="1.0"?><ErrorResponse xmlns="http://queue.amazonaws.com/doc/2012-11-05/"><Error><Type>Sender</Type><Code>I (truncated...)
 InvalidClientTokenId (client): The security token included in the request is invalid. - <?xml version="1.0"?><ErrorResponse xmlns="http://queue.amazonaws.com/doc/2012-11-05/"><Error><Type>Sender</Type><Code>InvalidClientTokenId</Code><Message>The security token included in the request is invalid.</Message><Detail/></Error><RequestId>1ef4235d-f932-578c-9c34-77734f141e56</RequestId></ErrorResponse> {"exception":"[object] (Aws\\Sqs\\Exception\\SqsException(code: 0): Error executing \"ReceiveMessage\" on \"https://sqs.us-east-1.amazonaws.com/your-account-id/your-queue-name\"; AWS HTTP error: Client error: `POST https://sqs.us-east-1.amazonaws.com/your-account-id/your-queue-name` resulted in a `403 Forbidden` response:
<?xml version=\"1.0\"?><ErrorResponse xmlns=\"http://queue.amazonaws.com/doc/2012-11-05/\"><Error><Type>Sender</Type><Code>I (truncated...)
 InvalidClientTokenId (client): The security token included in the request is invalid. - <?xml version=\"1.0\"?><ErrorResponse xmlns=\"http://queue.amazonaws.com/doc/2012-11-05/\"><Error><Type>Sender</Type><Code>InvalidClientTokenId</Code><Message>The security token included in the request is invalid.</Message><Detail/></Error><RequestId>1ef4235d-f932-578c-9c34-77734f141e56</RequestId></ErrorResponse> at /var/www/html/vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php:192, GuzzleHttp\\Exception\\ClientException(code: 403): Client error: `POST https://sqs.us-east-1.amazonaws.com/your-account-id/your-queue-name` resulted in a `403 Forbidden` response:
<?xml version=\"1.0\"?><ErrorResponse xmlns=\"http://queue.amazonaws.com/doc/2012-11-05/\"><Error><Type>Sender</Type><Code>I (truncated...)
 at /var/www/html/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113)
[stacktrace]
#0 /var/www/html/vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php(101): Aws\\WrappedHttpHandler->parseError(Array, Object(GuzzleHttp\\Psr7\\Request), Object(Aws\\Command), Array)
#1 /var/www/html/vendor/guzzlehttp/promises/src/Promise.php(203): Aws\\WrappedHttpHandler->Aws\\{closure}(Array)
#2 /var/www/html/vendor/guzzlehttp/promises/src/Promise.php(174): GuzzleHttp\\Promise\\Promise::callHandler(2, Array, Array)
#3 /var/www/html/vendor/guzzlehttp/promises/src/RejectedPromise.php(40): GuzzleHttp\\Promise\\Promise::GuzzleHttp\\Promise\\{closure}(Array)
#4 /var/www/html/vendor/guzzlehttp/promises/src/TaskQueue.php(47): GuzzleHttp\\Promise\\RejectedPromise::GuzzleHttp\\Promise\\{closure}()
#5 /var/www/html/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(96): GuzzleHttp\\Promise\\TaskQueue->run()
#6 /var/www/html/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(123): GuzzleHttp\\Handler\\CurlMultiHandler->tick()
#7 /var/www/html/vendor/guzzlehttp/promises/src/Promise.php(246): GuzzleHttp\\Handler\\CurlMultiHandler->execute(true)
#8 /var/www/html/vendor/guzzlehttp/promises/src/Promise.php(223): GuzzleHttp\\Promise\\Promise->invokeWaitFn()
#9 /var/www/html/vendor/guzzlehttp/promises/src/Promise.php(267): GuzzleHttp\\Promise\\Promise->waitIfPending()
#10 /var/www/html/vendor/guzzlehttp/promises/src/Promise.php(225): GuzzleHttp\\Promise\\Promise->invokeWaitList()
#11 /var/www/html/vendor/guzzlehttp/promises/src/Promise.php(267): GuzzleHttp\\Promise\\Promise->waitIfPending()
#12 /var/www/html/vendor/guzzlehttp/promises/src/Promise.php(225): GuzzleHttp\\Promise\\Promise->invokeWaitList()
#13 /var/www/html/vendor/guzzlehttp/promises/src/Promise.php(62): GuzzleHttp\\Promise\\Promise->waitIfPending()
#14 /var/www/html/vendor/aws/aws-sdk-php/src/AwsClientTrait.php(59): GuzzleHttp\\Promise\\Promise->wait()
#15 /var/www/html/vendor/aws/aws-sdk-php/src/AwsClientTrait.php(78): Aws\\AwsClient->execute(Object(Aws\\Command))
#16 /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/SqsQueue.php(120): Aws\\AwsClient->__call('receiveMessage', Array)
#17 /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(244): Illuminate\\Queue\\SqsQueue->pop('https://sqs.us-...')
#18 /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(105): Illuminate\\Queue\\Worker->getNextJob(Object(Illuminate\\Queue\\SqsQueue), 'your-queue-name')
#19 /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(101): Illuminate\\Queue\\Worker->daemon('sqs', 'your-queue-name', Object(Illuminate\\Queue\\WorkerOptions))
#20 /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(85): Illuminate\\Queue\\Console\\WorkCommand->runWorker('sqs', 'your-queue-name')
#21 [internal function]: Illuminate\\Queue\\Console\\WorkCommand->handle()
#22 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array(Array, Array)
#23 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#24 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#25 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(549): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#26 /var/www/html/vendor/laravel/framework/src/Illuminate/Console/Command.php(180): Illuminate\\Container\\Container->call(Array)
#27 /var/www/html/vendor/symfony/console/Command/Command.php(252): Illuminate\\Console\\Command->execute(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#28 /var/www/html/vendor/laravel/framework/src/Illuminate/Console/Command.php(167): Symfony\\Component\\Console\\Command\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#29 /var/www/html/vendor/symfony/console/Application.php(936): Illuminate\\Console\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#30 /var/www/html/vendor/symfony/console/Application.php(240): Symfony\\Component\\Console\\Application->doRunCommand(Object(Illuminate\\Queue\\Console\\WorkCommand), Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#31 /var/www/html/vendor/symfony/console/Application.php(148): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#32 /var/www/html/vendor/laravel/framework/src/Illuminate/Console/Application.php(88): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#33 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(121): Illuminate\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#34 /var/www/html/artisan(37): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#35 {main}
"}

因此,我通过从sqs的{​​{1}}中删除command来解决了这个问题,因此它使用默认的队列连接来启动队列:

/etc/supervisor/conf.d/laravel-worker.conf

然后重新启动主管:

command=php /home/forge/app.com/artisan queue:work --sleep=3 --tries=3

Redis

我还试图存储Redis日志文件,由于某种未知的原因,该文件在启动Redis时会产生错误。因此,我只是决定不再存储日志文件,方法是在Redis配置(sudo supervisorctl reread sudo supervisorctl update sudo supervisorctl start laravel-worker:* )中将logfile留空。 PS .:我已经将Ubuntu 16.04的Redis安装并配置为described here