WebSocket connection is not working when script is

2019-06-07 07:46发布

问题:

So, i have simple webSocket connection script:

$(document).ready(function() {
    // set web scocket connection
    var wsConnection = new WebSocket('wss://websocket.lh:4443');
    // web socket on open action
    wsConnection.onopen = function() {
        // web socket
        wsConnection.send(JSON.stringify({
            'action': 'subscribe',
            'channel': '{{ app.user.webSocketToken }}'
        }));
    }
    // web socket on recive data action
    wsConnection.onmessage = function(e) {
        // load notifications handler
        requirejs(["notificationsHandler"], function() {
            fn_notificationsHandler.getNotify(e);
        });
    };
});

This is working fine, but only when it's directly in my index/base.html, when i'm trying to include it as a external script like bellow the onmessage method stops working.

<script src="/js/app/wsActions.js"></script>

Also when i'm trying to do this with Require.js by typical way also not working. Why is that? Can anybody help?