【问题标题】:node.js "Answering questions" : broadcast out to the rest of the connected clientsnode.js“回答问题”:向其他连接的客户端广播
【发布时间】:2012-06-23 23:23:55
【问题描述】:

我正在尝试:

在客户端上为“answer”事件构建监听,这将向我们发送问题和答案,我们希望将其广播给其他连接的客户端。

app.js =

var express = require('express');
var app = express.createServer();
var socket = require('socket.io');
var io = socket.listen(app);

io.sockets.on('connection', function(client) {
    console.log("Client connected...");

    // listen for answers here

   client.on('question', function(question) {
       client.get('question_asked', function(asked) {
           if(!asked) {
               client.set('question_asked', true);
               client.broadcast.emit('question', question);
           }
       });
   });
});

【问题讨论】:

  • 我试试:client.on('answer', function(data){ client.broadcast.emit("answer", data); });
  • 问题在哪里?有什么问题?

标签: jquery json node.js real-time javascript-framework


【解决方案1】:

您可以使用hook.io 与所有客户交谈。 This 可能是一个很好的起点。

【讨论】:

  • 哦,谢谢。但明白了。如果有人在看,解决方案是:client.on('answer', function(a,b) { client.broadcast.emit("answer", a, b); });
猜你喜欢
  • 2018-01-09
  • 1970-01-01
  • 2017-05-26
  • 2020-03-12
  • 1970-01-01
  • 1970-01-01
  • 2016-12-02
  • 2014-12-15
  • 1970-01-01
相关资源
最近更新 更多