简体   繁体   English

如何将 swoole(php 的 websocket 扩展)上的消息从 php 发送到浏览器?

[英]How send message on swoole (websocket extension for php) from php to browser?

This file run my server这个文件运行我的服务器

<?php

class websocket
{
    public $ws;

    public function start()
    {
        $this->ws = new swoole_websocket_server('127.0.0.1', 9502);

        $this->ws->on('open', function ($ws, $request) {
            echo "connection open: {$request->fd}\n";
        });
        $this->ws->on('message', function ($ws, $frame) {
            echo "received message: {$frame->data}\n";
            $this->ws->push($frame->fd, json_encode(["hello", "world"]));
        });
        $this->ws->on('close', function ($ws, $id) {
            $this->onClose($id);
        });

        $this->ws->start();
        $this->sendMessage(1, "asdfasdf");
    }

    public function sendMessage($id,$msg)
    {
        $this->ws->push($id, "asdf");
    }
}

I run it from cli like this:我像这样从 cli 运行它:

php -r 'include("websocket.php"); $web = new websocket; $web->start();' 

then I open on browser this file然后我在浏览器上打开这个文件

<?php
include ('websocket.php');
$n = new websocket();
$n->ws->push(1, "asdf",  1,  true);

and I get this error:我收到这个错误:

127.0.0.1:51180 [500]: GET /send.php - Uncaught Error: Call to a member function push() on null in /home/ganbatte/Desktop/123/send.php:4 127.0.0.1:51180 [500]: GET /send.php - 未捕获的错误:调用成员函数 push() on null in /home/ganbatte/Desktop/123/send.php:4

Why is that and how can I fix it?为什么会这样,我该如何解决?

Right after instanciating the object the $ws property does not have any value yet.在实例化对象之后, $ws属性还没有任何值。 Yet you try to access it.然而你试图访问它。 It looks like you have to start it first, like this:看起来你必须先启动它,像这样:

include ('websocket.php');
$n = new websocket();
$n->start(); // <-- add this line
$n->ws->push(1, "asdf",  1,  true);

However, given that there is a sendMessage() method as well, I guess you should probably use that, but I am not deep into swoole at all.但是,鉴于还有一个sendMessage()方法,我想您可能应该使用它,但我根本没有深入了解 swoole。

This looks like the docs you are looking for: Get Started with Swoole And maybe it is a good idea to read up on the systematic basics there too.这看起来像您正在寻找的文档: Swoole 入门也许阅读那里的系统基础知识也是个好主意。

Remember that this send method sends a message "on" the websocket to the attached clients, not from the clients to the server (a client, most likely a browser, has to do that part).请记住,此发送方法在 websocket 上向附加的客户端发送消息,而不是从客户端发送到服务器(客户端,很可能是浏览器,必须执行该部分)。

This code snippet explains sendMessage() and push to fds in scopes此代码片段解释了 sendMessage() 和在范围内推送到 fds

`$server->fds = [];

 $server->on('open', function (swoole_websocket_server $server, $request) 
 {
  // add fd to scope
   $server->fds[$request->fd] = true; // dummy-bool
 });



$server->on('close', function ($server, $fd) {
// delete fd from scope
unset($server->fds[$fd]);
});

$server->on('message', function (swoole_websocket_server $server, $frame) 
{
 $message = "simple test message number: " . rand(1,10000);
 // pipe the message to all 9 other workers
 for ($i=0; $i < 10; $i++) { // WORKER_NUM
 if ($i !== $server->worker_id)
 // in this case only workers (no taskworkers)
  $server->sendMessage($message, $i); 
 }
// message to all fds in this scope
testMessageSender($server, $message);
});

$server->on('pipeMessage', function($server, $src_worker_id, $data) {
  // send to your known fds in worker scope
 testMessageSender($server, $data);
});

function testMessageSender(&$server, $message){
 // use only your own scope fds
 foreach ($server->fds as $fd => $dummyBool) {
  // push to your connected clients
  $server->push($fd, $message);
  }
}`

Reference:参考:

Swoole Official Discussion Swoole 官方讨论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM