从外部源加载脚本时,WebSocket连接不起作用

所以,我有简单的webSocket连接脚本:

$(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); }); }; }); 

这工作正常,但只有当它直接在我的index / base.html中时,当我试图将它作为外部脚本包含在下面时onmessage方法停止工作。

此外,当我试图通过典型的方式与Require.js这样做也无法正常工作。 这是为什么? 有人可以帮忙吗?