Has Phonegap integrate socket.io in version 3.5.0-

2019-05-10 23:54发布

问题:

Since I've update my PhoneGap to version 3.5.0-0.20.10 I get problems in my project.

When I'm running the PhoneGap service, then I see in the cli;

[phonegap] 200 /socket.io/?EIO=2&transport=polling&t=.......

But I don't use socket.io. Using the Chrome developer tools I see that in my project is a socket.io folder with a socket.io.js. So I think PhoneGap import it by itself.

And my RequireJS have now a problem:

Uncaught ReferenceError: io is not defined

I tried to remove RequireJS, then it works but I need RequireJS. All works perfectly on the older version 3.5.0-0.20.5

回答1:

Found the answer ... Modify consoler.js under phonegap's node-modules directory (typically /usr/local/lib/node_modules/phonegap/node_modules/connect-phonegap/res/middleware) to load socket.io through AMD rather than directly:

<!-- <script src="/socket.io/socket.io.js"></script> -->
<script>
(function(window) {
    require(['/socket.io/socket.io.js'],function(io){
        var socket = io('http://' + document.location.host);
        var previousConsole = window.console || {};
        window.console = {
            log:function(msg){
                previousConsole.log && previousConsole.log(msg);
                socket.emit('console','log', msg);
            },
            warn:function(msg){
                previousConsole.warn && previousConsole.warn(msg);
                socket.emit('console','warn', msg);
                },
            error:function(msg){
                previousConsole.error && previousConsole.error(msg);
                socket.emit('console','error', msg);
            }
        }
    });
})(window);
</script>