在我的laravel 5.7应用程序中,我使用MessageComponentInterface与类似的聊天应用程序
<?php
namespace App\Classes\Socket;
use App\Classes\Socket\Base\BaseSocket;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class ChatSocket extends BaseSocket
{
protected $clients;
public function __construct()
{
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
echo '<pre>New Connection ::'.print_r($conn->resourceId,true).'</pre>';
}
public function onMessage(ConnectionInterface $from, $msg) {
$numRecv= count($this->clients)-1;
echo '<pre>onMessage $msg::' . print_r( $msg, true ) ;
// var_dump($from);
dump($from);
die("-1 XXZ000");
echo '<pre>onMessage $from::' . print_r( $from, true ) ;
...
Run Code Online (Sandbox Code Playgroud)
问题是在onMessage事件中我希望将此消息写入db表,但是我找不到从哪里获取发送此消息的用户的user_id?我尝试将输出值调试到屏幕上
echo '<pre>onMessage $from::' . print_r( $from, true ) ;
Run Code Online (Sandbox Code Playgroud)
但我得到了memmory错误,但在/etc/php/7.2/cli/php.ini我修改了选项:
max_execution_time = 3330
max_input_time = 240
memory_limit = 4048M
Run Code Online (Sandbox Code Playgroud)
并重新启动我的服务器,但无论如何我得到了内存错误
使用这些方法:
// var_dump($from);
dump($from);
Run Code Online (Sandbox Code Playgroud)
我在控制台得到了无穷无尽的输出,我无法抓住任何东西......
如何调试这些值?
更新#2 我在https://laravel.io/forum/01-16-2015-loading-laravels-session-using-ratchet 文章中尝试了示例 ,但是当我尝试使用方法运行它时:
public function onMessage(ConnectionInterface $from, $msg) {
$from->session->start();
$idUser = $from->session->get(Auth::getName());
Run Code Online (Sandbox Code Playgroud)
我收到了错误:
Undefined property: Ratchet\Server\IoConnection::$session
Run Code Online (Sandbox Code Playgroud)
在上述文章中有评论:
(您必须解密cookie才能获得Laravel 5中的会话ID):
我找到了这个我找到了这个
在Web请求上下文中,Cookie通常由EncryptCookies中间件自动加密和解密.所以最简单的选择就是启用这个中间件(并且它在Laravel中默认启用).
在我的app/Http/Kernel.php中,有一行使用EncryptCookies
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
Run Code Online (Sandbox Code Playgroud)
但这是Web应用程序,但我运行控制台应用程序.错误的原因我必须将EncryptCookies添加到app/Http/Kernel.php中的其他组吗?或者为什么错误?
同样在file:///etc/php/7.2/cli/php.ini中我修改了
memory_limit = 8048M
Run Code Online (Sandbox Code Playgroud)
并重新启动apache
我也尝试检查脚本中有多少内存:
echo '<pre>onMessage getNiceFileSize(memory_get_usage()) ::' . print_r( $this->getNiceFileSize(memory_get_usage()) , true ) ;
it shows :19241736 ~18 MiB
Run Code Online (Sandbox Code Playgroud)
错误信息 :
Out of memory (allocated 1623195648) (tried to allocate 1600131072 bytes)
Run Code Online (Sandbox Code Playgroud)
我试图计算并得到值1.51 GiB和1.49 GiB ...
谢谢!
您看到这篇https://laravel.io/forum/01-16-2015-loading-laravels-session-using-ratchet文章了吗?它有类似的例子:
public function onMessage(ConnectionInterface $from, $msg) {
// start the session when the user send a message
// (refreshing it to be sure that we have access to the current state of the session)
$from->session->start();
// do what you wants with the session
// for example you can test if the user is auth and get his id back like this:
$idUser = $from->session->get(Auth::getName());
if (!isset($idUser)) {
echo "the user is not logged via an http session";
} else {
$currentUser = User::find($idUser);
}
// or you can save data to the session
$from->session->put('foo', 'bar');
// ...
// and at the end. save the session state to the store
$from->session->save();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
79 次 |
最近记录: |