Appsync 的 graphql 订阅不适用于 PHP5
Posted
技术标签:
【中文标题】Appsync 的 graphql 订阅不适用于 PHP5【英文标题】:Appsync's graphql subscription not working with PHP5 【发布时间】:2020-10-27 11:44:23 【问题描述】:谁能帮我提供一个使用 php5 和 AWS Appsync 或 php5 WebSocket 客户端(与 Appsync 一起使用)订阅 graphql 的工作示例? 我正在尝试textalk,但没有成功。
<?php
require(dirname(dirname(__FILE__)) . '/graphql/vendor/autoload.php');
use WebSocket\Client;
$query = <<<'GRAPHQL'
subscription onCreateProfile
onCreateBatch
hotelId
batchId
profiles
id
GRAPHQL;
$appSyncURL = 'wss://pbnblnr7xxxxxxxxxxxx.appsync-realtime-api.ap-south-1.amazonaws.com/graphql';
$context = stream_context_create();
stream_context_set_option($context, 'ssl', 'verify_peer', false);
stream_context_set_option($context, 'ssl', 'verify_peer_name', false);
$client = new WebSocket\Client($appSyncURL, [
'timeout' => 60, // 1 minute time out
'context' => $context,
'headers' => [
'x-api-key' => 'APIKEY',
],
]);
$client->send($query);
echo $client->receive();
$client->close();
在 sn-p 上运行会出现以下错误:
$ php subscription.php
"payload":"errors":["message":"NoProtocolError","errorCode":400],"type":"connection_error"PHP Fatal error: Uncaught WebSocket\ConnectionException: Empty read; connection dead? Stream state: "crypto":"protocol":"UNKNOWN","cipher_name":"TLS_AES_256_GCM_SHA384","cipher_bits":256,"cipher_version":"TLSv1.3","timed_out":false,"blocked":true,"eof":true,"stream_type":"tcp_socket\/ssl","mode":"r+","unread_bytes":0,"seekable":false in /var/www/html/demo/graphql/vendor/textalk/websocket/lib/Base.php:316
Stack trace:
#0 /var/www/html/demo/graphql/vendor/textalk/websocket/lib/Base.php(299): WebSocket\Base->throwException('Empty read; con...')
#1 /var/www/html/demo/graphql/vendor/textalk/websocket/lib/Base.php(159): WebSocket\Base->read(2)
#2 /var/www/html/demo/graphql/vendor/textalk/websocket/lib/Base.php(149): WebSocket\Base->receiveFragment()
#3 /var/www/html/demo/graphql/vendor/textalk/websocket/lib/Base.php(272): WebSocket\Base->receive()
#4 /var/www/html/demo/graphql/subscription.php(31): WebSocket\Base->close()
#5 main
thrown in /var/www/html/demo/graphql/vendor/textalk/websocket/lib/Base.php on line 316
那么有没有人尝试过使用 php5 订阅 graphQL?我的要求是在 PHP 应用程序(后端)中使用 Graphql 订阅。我知道 graphQL 订阅可以通过 javascript WebSocket 客户端在前端正常工作。但我必须在 PHP5 应用程序中使用 graphql 订阅。
任何帮助将不胜感激!
【问题讨论】:
【参考方案1】:我已经弄清楚了,现在它在 >= PHP 5.6 上运行良好。
这是一个相同的工作示例,以防万一有人需要。
https://github.com/roshanlabh/php-appsync-consumer-example/blob/master/appsync-subscription.php
【讨论】:
github.com/Textalk/websocket-php#example-listening-to-a-server 遗漏了什么?以上是关于Appsync 的 graphql 订阅不适用于 PHP5的主要内容,如果未能解决你的问题,请参考以下文章
AppSync/Amplify - 如何定义 GraphQL 订阅
Appsync 客户端对 GraphQL 突变的 Angular 订阅
放大/反应 AppSync graphql 订阅 @auth 错误