与node.js socket.io的Dojox套接字异常

时间:2013-06-06 03:48:46

标签: javascript websocket socket.io dojo

我正在使用dojo 1.8.3版本。尝试在服务器上使用带有node.js socket.io的dojox套接字。 客户端代码是

<!DOCTYPE HTML>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Demo: Basic Programmatic Chart</title>
        <link rel="stylesheet" href="style.css" media="screen">
        <link rel="stylesheet" href="../../../resources/style/demo.css" media="screen">
    </head>
    <body>
        <!-- create the chart -->
        <div id="chartNode" style="width: 550px; height: 550px;"></div>

        <!-- load dojo and provide config via data attribute -->
        <!-- load dojo and provide config via data attribute -->
        <script src="http://localhost/dojo-release-1.8.3/dojo/dojo.js"></script>

<script>
dojo.ready( function(){
require([
    "dojo/request", "dojox/socket"
], function(request, Socket){
    var
        args = {},
        ws = typeof WebSocket != "undefined",
        url =  ws ? "/socket.io/websocket" : "/socket.io/xhr-polling";

    var socket = new Socket(args = {
        url:url,
        headers:{
            "Content-Type":"application/x-www-urlencoded"
        },
        transport: function(args, message){
            args.data = message; // use URL-encoding to send the message instead of a raw body
            request.post(url, args);
        }
    });
    var sessionId;
    socket.on("message", function(){
        if (!sessionId){
            sessionId = message;
            url += '/' + sessionId;
        }else if(message.substr(0, 3) == '~h~'){
            // a heartbeat
        }
    });
});
});
</script>

创建websocket时(使用chrome版本27.0.1453.94 m),它会抛出异常“Uncaught TypeError:无法调用未定义的dojo.js的方法'createEvent':15306” 调用堆栈是:

on.emit dojo.js:15306
Socket.replace socket.js:78
target.(anonymous function).dispatcher dojo.js:17098
on.emit dojo.js:15270
on.emit dojo.js:15317
fire socket.js:205
connect

请让我知道如何让它发挥作用。

1 个答案:

答案 0 :(得分:0)