WebSocket connection is not working when script is loaded from external source

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 03:43:20

问题


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?

来源:https://stackoverflow.com/questions/34719970/websocket-connection-is-not-working-when-script-is-loaded-from-external-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!