socket.io基本问题

时间:2013-03-05 17:07:18

标签: socket.io

我在app.js中有这段代码

var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
static = require('node-static');
var fileServer = new static.Server('./');
app.listen(8080);
function handler (request, response) {
    request.addListener('end', function () {
        fileServer.serve(request, response);
    });
}
io.set('log level', 1);
io.sockets.on('connection', function (socket) {
     socket.on('click', function () {
            socket.broadcast.emit('moving');
        });

});

的index.html

<script>
    var url = window.location.hostname;
    var socket = io.connect(url);
     socket.on('moving', function () {
        $("body").append('<div class="text">tex</div>');
     });
</script>

我想写简单的文本(div),但它不起作用......哪里可能有问题?

1 个答案:

答案 0 :(得分:1)

默认情况下,

socket.io无助于观察click事件。 编辑您的客户端代码,如下所示:

<script>
    var url = window.location.hostname;
    var socket = io.connect(url);
    socket.on('moving', function () {
        $("body").append('<div class="text">tex</div>');
    });
    // manually emit the event, or bind `click` inside dom
    // and call socket.emit('click') inside onClick()
    socket.emit('click');
</script>