WebSocket connection failed and Uncaught DOMException errors

139 views Asked by At

I am trying to use chat application on Amazon AWS using IVS resources. I am using CreateChatToken.php and index.html pages that I wrote below. However,I saw these two errors in console: Uncaught DOMException: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state. and WebSocket connection to 'ws://edge.ivschat.eu-west-1.amazonaws.com/' failed I'm trying to open my index.html file on the browser on chrome. I tried changing the region on the code and replacing wss with ws but the errors I got did not change.

(This page is CreateChatToken.php)

<?php

include "./vendor/autoload.php";
$accessKey = "<ACCESS KEY EXAMPLE>";
$secretKey="<SECRET KEY EXAMPLE>";
$region = "eu-west-1";
$awsclient = new Aws\ivschat\ivschatClient([
'version' => 'latest',
 'region'  => $region,
'credentials' => array(
  'key'    => $accessKey,
 'secret' => $secretKey,
    )
]);
 'roomIdentifier' => '<ROOM IDENTIFIER EXAMPLE>',
 'userId' => '11111',
]);
var_dump($result);

(This page is index.html)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    const chatClientToken = "<CHAT TOKEN EXAMPLE>";
    const socket = "wss://edge.ivschat.eu-west-1.amazonaws.com";
const connection = new WebSocket(socket, chatClientToken);
    const payload = {
        "Action": "SEND_MESSAGE",
        "RequestId": "OPTIONAL_ID_YOU_CAN_SPECIFY_TO_TRACK_THE_REQUEST",
        "Content": "text message",
        "Attributes": {
            "CustomMetadata": "test metadata"
}
}

    connection.send(JSON.stringify(payload));
    connection.onmessage = (event) => {
        const data = JSON.parse(event.data);
        displayMessages({
            display_name: data.Sender.Attributes.DisplayName,
            message: data.Content,
            timestamp: data.SendTime
        });


}


    function displayMessages(message) {
        console.log(message);
    }
</script>
</html>
0

There are 0 answers