如何将消息发送到特定的connectionId到AWS API Gateway Websockets?

时间:2019-10-26 15:13:29

标签: php amazon-web-services websocket aws-api-gateway

我正在使用此代码在用户设备上建立新连接。

var socket = new WebSocket("wss://cdsbxtx2xi.execute-api.us-east-2.amazonaws.com/test"); 
socket.onmessage = function (event) {
  json = JSON.parse(event.data);
  connectionId = json.connectionId;
  document.cookie = "connection_id="+connectionId;
  console.info(json);
}

假设从此请求中我获得connectionId CLO5bFP1CYcFSbw =

另一个设备上的另一个用户也使用connectionId Cs42Fs5s5yuSbc =建立了新连接。现在如何从用户2设备向用户1发送消息?

我已经尝试过了。我不知道这是不是正确的方法,但是我仍然愿意提出任何建议。

use Aws\Signature\SignatureV4;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use Aws\Credentials\Credentials;
$client =  new GuzzleHttp\Client();
$credentials = new Credentials("XXXXXXXXXX","XXXXXXXX");
$url = "https://cdsbxtx2xi.execute-api.us-east-2.amazonaws.com/test/@connections/CLO5bFP1CYcFSbw=";
$region = 'us-east-2'; 
$msg['action'] = 'sendmessage';
$msg['data'] = 'hello world';
$msg = json_encode($msg);
$request = new Request('POST', $url, '["json"=>$msg]');

$s4 = new SignatureV4("execute-api", $region);
$signedrequest = $s4->signRequest($request, $credentials); 
$response = $client->send($signedrequest);
echo $response->getBody();

此代码不断加载,最终引发网关超时错误。

我希望用户2应该能够通过wss或https将消息发送到任何特定的connectionId。

我通过签署此请求来尝试使用https,但签署不起作用。签名部分出现错误

2 个答案:

答案 0 :(得分:0)

在过去3天中为这个问题苦苦挣扎后,终于找到了解决方案。以前在StackOverflow上提到的解决方案都没有对我有用。

这是正确的解决方案。我希望这会对某人有所帮助。

use Aws\Signature\SignatureV4;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use Aws\Credentials\Credentials;
$client =  new GuzzleHttp\Client();
$credentials = new Credentials(accessKeyId, secretAccessKey);
$url = "https://xsdsdsd.execute-api.us-east-2.amazonaws.com/test/@connections/CNtBveH2iYcCKrA="; 
// CNtBveH2iYcCKrA= is connectionid
$region = 'us-east-2'; 
$msg['action'] = 'sendmessage';
$msg['data'] = 'hello world'; 
$msg = json_encode($msg); 
$headers = array('Content-Type => application/x-www-form-urlencoded');
$request = new GuzzleHttp\Psr7\Request('POST', $url, ['Content-Type' => 'application/json'], $msg); 
$signer = new Aws\Signature\SignatureV4('execute-api', $region); 
$request = $signer->signRequest($request, $credentials);
$headers = array('Content-Type => application/x-www-form-urlencoded');
$client = new \GuzzleHttp\Client([ 'headers' => $headers]);
$response = $client->send($request);
$result = $response->getBody();

答案 1 :(得分:0)

嘿,您也可以使用 Connection URL 发送消息。

连接网址:https://{api-id}.execute-api.us-east-1.amazonaws.com/{stage}/@connections

要找到,请访问:Aws console > Api gateway > api > your_api > dashboard 他们,您将找到您的连接网址。

使用 php cURL 方法,因为它的 easyfastGuzzleHttp 相比

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://{api-id}.execute-api.us-east-1.amazonaws.com/{stage}/@connections/{YOUR_CONNECTION_ID_OF_USER}',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{"message" : "Hello world"}',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;