angularjs - ReferenceError: io is not defined - when socket server is different than localhost -
a lot of questions same title 1 different not find in existing stack overflow collection.
i have socket server @ different host , want use socket server web app.
i tried use inside index.html file of angular application way: index.html
<head> <link rel="stylesheet" href="onecss.css"> </head> <body ng-app="myapp"> <div ui-view></div> <script ng-href="{{socketurl}}"></script> <!-- $rootscope.socketurl = "https://example.com/socket.io/socket.io.js"; --> <script src="bower_components/angular-socket-io/socket.js"></script> <script src="commsocket.js"></script> <script src="onejs.js"></script> </body> commsocket.js
(function(app) { 'use strict'; app.factory('commsocket', function (socketfactory) { var socketurl = "https://example.com"; var iosocket = io.connect(socketurl); // here io throws error. referenceerror: io not defined var options = { iosocket: iosocket }; var socket = socketfactory(options); socket.forward('broadcast'); return socket; }); })(app); i think time https://example.com/socket.io/socket.io.js loads, commsocket gets called before , throws error. question how can wait socket.io.js file load before commsocket gets called.
this works fine if connect local socket.
Comments
Post a Comment