首页
下载
文档
问答社区
视频
捐赠
源代码
AI 助理
赞助商
CRMEB
Apipost
腾讯云
微擎
禅道
51Talk
商业产品
Swoole AI 智能文档翻译器
Swoole-Compiler PHP 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
请问这两种push有什么区别
$server->push($frame->fd, "this is server"); $this->server->push($frame->fd, "this is server"); 代码中这两种push区别在哪儿呢? 我测试了下两个对象 json_encode 是相等的 {{{ <?php class WebsocketTest { public $server; public function __construct() { $this->server = new swoole_websocket_server("0.0.0.0", 9501); $this->server->on('open', function (swoole_websocket_server $server, $request) { echo "server: handshake success with fd{$request->fd}\n"; }); $this->server->on('message', function (swoole_websocket_server $server, $frame) { echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n"; $encode1= json_encode($server); $encode2= json_encode($this->server); if($encode1 != $encode2){ echo '有区别'; } $server->push($frame->fd, "this is server"); $this->server->push($frame->fd, "this is server"); }); $this->server->on('close', function ($ser, $fd) { echo "client {$fd} closed\n"; }); $this->server->on('request', function ($request, $response) { // 接收http请求从get获取message参数的值,给用户推送 // $this->server->connections 遍历所有websocket连接用户的fd,给所有用户推送 foreach ($this->server->connections as $fd) { $this->server->push($fd, $request->get['message']); } }); $this->server->start(); } } new WebsocketTest(); ?> }}}
发布于5年前 · 1 次浏览 · 来自
提问
I
Iveon
$server->push($frame->fd, "this is server"); $this->server->push($frame->fd, "this is server"); 代码中这两种push区别在哪儿呢? 我测试了下两个对象 json_encode 是相等的 {{{ <?php class WebsocketTest { public $server; public function __construct() { $this->server = new swoole_websocket_server("0.0.0.0", 9501); $this->server->on('open', function (swoole_websocket_server $server, $request) { echo "server: handshake success with fd{$request->fd}\n"; }); $this->server->on('message', function (swoole_websocket_server $server, $frame) { echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n"; $encode1= json_encode($server); $encode2= json_encode($this->server); if($encode1 != $encode2){ echo '有区别'; } $server->push($frame->fd, "this is server"); $this->server->push($frame->fd, "this is server"); }); $this->server->on('close', function ($ser, $fd) { echo "client {$fd} closed\n"; }); $this->server->on('request', function ($request, $response) { // 接收http请求从get获取message参数的值,给用户推送 // $this->server->connections 遍历所有websocket连接用户的fd,给所有用户推送 foreach ($this->server->connections as $fd) { $this->server->push($fd, $request->get['message']); } }); $this->server->start(); } } new WebsocketTest(); ?> }}}
赞
0
分享
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
评论
2019-07-25
z
zzzzz
从 PHP 的语法上来说,前者($server)是一个变量,后者($this->server)是一个对象实例的属性。 具体在这段代码里,两者的值是相同的,都是代表一个 swoole_websocket_server 对象实例,所以当然结果是相同的喽。
赞
0
回复
微信公众号
热门内容
作者其它话题
- zif_swoole_server_taskwait: open(") failed. Error: No such file or directory[2]
暂无回复的问答
- CodeGalaxy K3s 轻量集群节点之间如何实现负载均衡
- 关于openssl CURL WARNING swSSL_connect: SSL_connect(fd=69) failed. Error: error:141A318A:SSL routines:tls_process_ske_dhe:dh key too small[1|394]
- 多个模型如何进行事务异常回退?
- websocket开启wss报错
- 协程tcp服务器如何使用多进程?recv()方法接收信息,打印出来的pid一直是同一个。没用使用到多进程啊。