Javascript 在onOpen函数中返回数组

Javascript 在onOpen函数中返回数组,javascript,laravel,websocket,ratchet,Javascript,Laravel,Websocket,Ratchet,我目前正在编写一个聊天应用程序。到目前为止,用户可以互相发送消息,我可以将消息存储在我的数据库中。现在,当用户打开页面向其他用户发送消息时,我想在我的onOpen函数中显示存储在数据库中的消息 这就是我的onOpen函数的外观: public function onOpen(ConnectionInterface $conn) { $query = $conn->httpRequest->getUri()->getQuery();

我目前正在编写一个聊天应用程序。到目前为止,用户可以互相发送消息,我可以将消息存储在我的数据库中。现在,当用户打开页面向其他用户发送消息时,我想在我的onOpen函数中显示存储在数据库中的消息

这就是我的onOpen函数的外观:

public function onOpen(ConnectionInterface $conn)
    {

        $query =  $conn->httpRequest->getUri()->getQuery();

        preg_match_all('!\d+!', $query, $matches);

        $my_id = $matches[0][0];

        $friend_id = $matches[0][1];

        $conn->resourceId = $my_id;

        $this->users[$conn->resourceId] = $conn;

        $messages = Private_message::where([

            ['user1',$my_id],
            ['user2',$friend_id]

        ])->orWhere([

            ['user1',$friend_id],
            ['user2',$my_id]

        ])->get()->toArray();

    }
我想将
$messages
数组返回到我的
privateChat.blade.php
视图,以便打印出来

Iv'e尝试执行
返回视图('privateChat',['messages'=>$messages])
@dd($messages)
在我的
privateChat.blade.php
中,但它表示变量未定义。
我还尝试了
返回$messages
,并在我的onopenjs函数中打印出来,但它不起作用。正确的方法是什么?

正如您所看到的,您没有返回任何内容以供查看

所以


如果有任何问题请在下面发表评论

尝试用
@dd($viewsharevalues)
打印出来,它给了我一个
未定义变量的错误:viewsharevalues
我想你误解了,你可以试试
@dd($messages)
,它被传递到
压缩文件()
并作为view Helper中的第二个参数传递问题是,我已经在另一个函数中返回了我的
privateChat
视图,并向其传递了变量,因此再次返回它不起作用,因为我已经在该视图中了。相反,我尝试将
$messages
数组与其他变量一起传递,结果成功了。
public function onOpen(ConnectionInterface $conn)
{

    $query =  $conn->httpRequest->getUri()->getQuery();

    preg_match_all('!\d+!', $query, $matches);

    $my_id = $matches[0][0];

    $friend_id = $matches[0][1];

    $conn->resourceId = $my_id;

    $this->users[$conn->resourceId] = $conn;

    $messages = Private_message::where([

        ['user1',$my_id],
        ['user2',$friend_id]

    ])->orWhere([

        ['user1',$friend_id],
        ['user2',$my_id]

    ])->get()->toArray();

   $viewShareVariables = compact('messages');

   return view('privateChat',$viewShareVariables);

}