Задать вопрос
@gitdev

Почему с Websocket постоянно не могу соеденится к серверу и постоянно получаю ошибку Uncaught DOMException: Failed to execute 'send' on 'WebSocket'?

Ошибка:
Uncaught DOMException: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state

Использую Ratchet

js
var newSock;
    function initChat()
    {
        alert('init the func');
        //let url = $('#chat').data('url');

        let socket = new WebSocket('ws://localhost:8080/');
        newSock = socket;
        socket.onmessage = function(event) {
            console.log(event.data);
            alert('text');
        }


        socket.onopen = function() {
            console.log('message ');
            alert('Соеденение установленно');
            socket.send('{userData: Token-user}');
        }
        socket.onerror = function (error) {
            alert('Error: ');
            console.log(error);
        }

    }
    function sendAMessage()
    {
        //alert('Send');
        newSock.send(JSON.stringify('Test message'));

        console.log(newSock.errorText);
    }


    function showMessage(message) {
        let messageEl = document.createElement('div');
        messageEl.appendChild(document.createTextNode(message));
        document.getElementById('#chat-messages').appendChild(messageEl);
    }

    $('#chat-messages-list').click(sendAMessage);

    initChat();


PHP
class ChatMessageService implements MessageComponentInterface {
    protected $clients;

    public function __construct() {

        $this->clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn) {
        // Store the new connection to send messages to later
        $this->clients->attach($conn);

        echo "New connection! ()\n";
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        $numRecv = count($this->clients) - 1;
        echo 'Connection %d sending message "%s" to %d other connection%s';

        foreach ($this->clients as $client) {
            if ($from !== $client) {

                // The sender is not the receiver, send to each client connected
                $client->send($msg);
            }
        }
    }

    public function onClose(ConnectionInterface $conn) {
        // The connection is closed, remove it, as we can no longer send it messages
        $this->clients->detach($conn);

        echo "Connection has disconnected\n";
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "An error has occurred: {$e->getMessage()}\n";

        $conn->close();
    }
}


Запуск сервера
$io = new SymfonyStyle($input, $output);

        echo '12345';

        //nc -l -p 8080
        $server = IoServer::factory(
            new HttpServer(
                new WsServer(
                    new ChatMessageService
                )
            ),
            8080
        );

        $server->run();
  • Вопрос задан
  • 116 просмотров
Подписаться 1 Простой Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы