1

I am trying to use Socket.io hosted by a Node server. I am using Require.js to manage dependencies. My webapp is Offline capable.

When the webapp is offline, and cannot contact the node server, require.js throws an error because it cannot find the socket.io dependency.

GET http://mikemac.local:8000/socket.io/socket.io.js  require.js:33
Uncaught Error: Script error
http://requirejs.org/docs/errors.html#scripterror

In this case I am not using node/io for the majority of the system, just 'bonus' real time notifications. So The app should run without it.

How can I deal with this? I would like a way to detect that it cannot be found and then disable the socket.io functionality until a connection/refresh attempt.

4

1 回答 1

0

在您的配置设置中回退到其他模块并在回退中指示它处于脱机状态:

requirejs.config({
    enforceDefine: true,
    paths: {
        socketio: [
            'http://mikemac.local:8000/socket.io/socket.io',
            //If the CDN location fails, load from this location
            'lib/socketoffline'
        ]
    }
});

//socketoffline
define({ offline: true});

//Later
require(['socketio'], function (socketio) {
    if (socketio.offline){
        // your library did not load:
    } else {
        // socketio loaded
    }
});
于 2013-05-25T18:47:42.777 回答