0

我正在尝试将coffeescriptsocket.io一起使用。

io = socketio.listen(server);
// handle incoming connections from clients
io.sockets.on('connection', function(socket) {
    // once a client has connected, we expect to get a ping from them saying what room they want to join
    socket.on('room', function(room) {
        socket.join(room);
    });
});

// now, it's easy to send a message to just the clients in a given room
room = "abc123";
io.sockets.in(room).emit('message', 'what is going on, party people?');

// this message will NOT go to the client defined above
io.sockets.in('foobar').emit('message', 'anyone in this room yet?'); 

io.sockets.in无法正确编译。

我应该如何解决这个问题?

4

1 回答 1

1

在您的问题中,您声明存在编译器错误,但在评论中您说没有。如果有,你真的应该发布你的咖啡脚本代码:)

我假设你在咖啡脚本中有这样的东西:

io = socketio.listen server

io.sockets.on 'connection', ->
    socket.on 'room', ->
        socket.join room

room = "abc123"
io.sockets.in(room).emit "message", "foobar"

io.sockets.in("foobar").emit "message", "barbaz"

编译为

io = socketio.listen(server);

io.sockets.on('connection', function() {
  return socket.on('room', function() {
    return socket.join(room);
  });
});

room = "abc123";

io.sockets["in"](room).emit("message", "foobar");

io.sockets["in"]("foobar").emit("message", "barbaz");

正如评论中所述,以下两行在 JavaScript 中是等价的:

io.sockets["in"](room).emit("message", "foobar");
io.sockets.in(room).emit("message", "foobar); 

您可以通过打开您最喜欢的 JavaScript 控制台来验证这一点:

> var test = { foo: "bar" }
> test.foo
'bar'
> test["foo"]
'bar'
于 2013-03-15T11:27:29.930 回答