Skip to content
This repository was archived by the owner on Sep 20, 2021. It is now read-only.
This repository was archived by the owner on Sep 20, 2021. It is now read-only.

Vue and websocket #94

Description

@YnievesDotNet

I try to use the demo code, on one app and I use vue in this app, but the code is broken and I recive this error

Uncaught TypeError: Cannot read property 'addEventListener' of null

Thanks.

Yoinier.

Activity

  1. Hywan commented on Jun 12, 2017

    @Hywan
    Member

    Maybe a little bit of code would help a lot :-).

  2. self-assigned this
    on Jun 12, 2017
  3. YnievesDotNet commented on Jun 12, 2017

    @YnievesDotNet
    Author

    That is my code

    ...
            <input type="text" id="input" placeholder="Message…" />
            <hr />
            <pre id="output"></pre>
    ...
    <script>
        var host   = 'ws://127.0.0.1:8000';
        var socket = null;
        var input  = document.getElementById('input');
        var output = document.getElementById('output');
        var print  = function (message) {
            var samp       = document.createElement('samp');
            samp.innerHTML = message + '\n';
            output.appendChild(samp);
    
            return;
        };
    
        input.addEventListener('keyup', function (evt) {
            if (13 === evt.keyCode) {
                var msg = input.value;
    
                if (!msg) {
                    return;
                }
    
                try {
                    socket.send(msg);
                    input.value = '';
                    input.focus();
                } catch (e) {
                    console.log(e);
                }
    
                return;
            }
        });
    
        try {
            socket = new WebSocket(host);
            socket.onopen = function () {
                print('connection is opened');
                input.focus();
    
                return;
            };
            socket.onmessage = function (msg) {
                print(msg.data);
    
                return;
            };
            socket.onclose = function () {
                print('connection is closed');
    
                return;
            };
        } catch (e) {
            console.log(e);
        }
    </script>
    <script src="/js/app.js"></script>
    

    Thanks, Yoinier.

  4. Pierozi commented on Jun 12, 2017

    @Pierozi
    Member

    Hello @YnievesDotNet
    This error means the DomElement input is not yet ready when the Javascript is executed.
    You should embed the javascript code on function and execute it after the onload event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions