使用GatewayWorker 报如下错误 什么原因

lyhchxl

workerman Workerman version:3.5.31 PHP version:7.1.30

2022-05-26 03:56:45 pid:2873481 Exception: You can't send(array) to client, you need to convert it to a string.  in /var/www/xunyin-php/vendor/workerman/workerman/Protocols/Websocket.php:236
Stack trace:
#0 /var/www/xunyin-php/vendor/workerman/workerman/Connection/TcpConnection.php(344): Workerman\Protocols\Websocket::encode(Array, Object(Workerman\Connection\TcpConnection))
#1 /var/www/xunyin-php/vendor/workerman/gateway-worker/src/Gateway.php(862): Workerman\Connection\TcpConnection->send(Array, false)
#2 /var/www/xunyin-php/vendor/workerman/workerman/Connection/TcpConnection.php(656): GatewayWorker\Gateway->onWorkerMessage(Object(Workerman\Connection\TcpConnection), Array)
#3 [internal function]: Workerman\Connection\TcpConnection->baseRead(Resource id #1441, 2, Resource id #1441)
#4 /var/www/xunyin-php/vendor/workerman/workerman/Events/Event.php(195): EventBase->loop()
#5 /var/www/xunyin-php/vendor/workerman/workerman/Worker.php(2408): Workerman\Events\Event->loop()
#6 /var/www/xunyin-php/vendor/workerman/gateway-worker/src/Gateway.php(276): Workerman\Worker->run()
#7 /var/www/xunyin-php/vendor/workerman/workerman/Worker.php(1541): GatewayWorker\Gateway->run()
#8 /var/www/xunyin-php/vendor/workerman/workerman/Worker.php(1371): Workerman\Worker::forkOneWorkerForLinux(Object(GatewayWorker\Gateway))
#9 /var/www/xunyin-php/vendor/workerman/workerman/Worker.php(1345): Workerman\Worker::forkWorkersForLinux()
#10 /var/www/xunyin-php/vendor/workerman/workerman/Worker.php(1675): Workerman\Worker::forkWorkers()
#11 /var/www/xunyin-php/vendor/workerman/workerman/Worker.php(1624): Workerman\Worker::monitorWorkersForLinux()
#12 /var/www/xunyin-php/vendor/workerman/workerman/Worker.php(548): Workerman\Worker::monitorWorkers()
#13 /var/www/xunyin-php/public/server.php(37): Workerman\Worker::runAll()
#14 {main}
1128 5 0
5个回答

抽不完的寂寞

改源码导致的?

  • 暂无评论
没茅台

不能发送数组,只能发送字符串!

  • 暂无评论
Tinywan

例外:您无法将(数组)发送到客户端,您需要将其转换为字符串

  • 暂无评论
li914

异常:无法将(数组)发送到客户端,需要将其转换为字符串。(来自百度翻译)

  • 暂无评论
phpcreeper

作为一个补充啰嗦几句:

You can't send(array) to client, you need to convert it to a string.

  • 首先这个异常提示何其人性化,不但告诉了原因:业务端不能传递数组类型参数,而且还提供了解决方案:将其转换为字符串就好了; 言外之意 + 严格意义上看:本例中因通信协议是websocket,其实现要求参数只能是标量类型的数据,比如string|float|int|boolean等等,所以说不一定是只能发送字符串,而是只要满足是标量类型数据即可。

  • 再进一步拓展下,其实业务端到底能发送什么类型的数据是和具体的应用层协议实现密切相关的,尤其是当我们实现自定义协议时,因为可以在应用协议层的encode方法内自由定制,从而可以支持各种数据类型,目前workerman内置的协议实现大多是支持标量类型数据或者干脆浓缩下就是字符串,有的还支持对象等等。

年代过于久远,无法发表回答
×
🔝