dojox没有定义

时间:2012-03-08 08:30:38

标签: websocket comet dojo

我正在尝试将COMET与dojo一起使用,并且在带有消息的示例的第一行失败

  

dojox未定义

我必须错过一些明显的东西

这是页面(错误在BODY的第一行):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
   <title></title>
   <script src="dojo/dojo.js"
           data-dojo-config="async:true, parseOnLoad:true">
   </script>
   <script>
      require( ["dojox/socket"] );
   </script>
</head>
<body>
<script type="text/javascript">
   var socket = dojox.socket( "/cometd" );
   function send( data ) {
      return socket.send( dojo.toJson( data ) );
   }
   socket.on( "connect", function () {
      // send a handshake
      send( [
         {
            "channel":"/meta/handshake",
            "version":"1.0",
            "minimumVersion":"1.0beta",
            "supportedConnectionTypes":["long-polling"] // or ["callback-polling"] for x-domain
         }
      ] )
      socket.on( "message", function ( data ) {
         // wait for the response so we can connect with the provided client id
         data = dojo.fromJson( data );
         if ( data.error ) {
            throw new Error( error );
         }
         // get the client id for all future messages
         clientId = data.clientId;
         // send a connect message
         send( [
            {
               "channel":"/meta/connect",
               "clientId":clientId,
               "connectionType":"long-polling"
            },
            {  // also send a subscription message
               "channel":"/meta/subscribe",
               "clientId":clientId,
               "subscription":"/foo/**"
            }
         ] );
         socket.on( "message", function ( data ) {
            alert( "message from server " + data )
         } );
      } );
   } );
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您正在混合使用AMD和旧式模块语法。新的AMD样式require为您的命名空间创建一个全局变量,就像旧dojo.require那样。

重写代码以完全使用新的AMD样式或删除在加载dojo时添加的async=true标志,以重新启用对旧模块样式的支持。

无论如何,用AMD风格重写它应该不难......

require([
    'dojo/_base/json', //_base is for things that used to be in the root dojo namespace.
    'dojox/socket'
],function(
    json,
    dojox_socket
){
    var socket = dojox_socket( "/cometd" );
    function send( data ) {
        return socket.send( json.toJson( data ) );
    }
    // and so on...
}); 

(顺便说一下,如果您在原始html中没有使用任何声明性小部件,也可以删除“parseOnLoad”标志)