Clients can also answer each other questions, so let's build that feature by first listening for the'answer' event on the client, which will send us both the question and answer, which we want to broadcast out to the rest of the connected clients.

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(err, asked) {
if(!asked) {
client.set('question_asked', true);
client.broadcast.emit('question', question);
}
});
//can pass two or more params in callback function
client.on('answer', function(question,answer) {
client.broadcast.emit('answer',question, answer);
});
});
});

Now on the client, listen for the 'answer' event and call the answerQuestion function, passing in both the question and the answer that was broadcasted from the server.

<script src="/socket.io/socket.io.js" />

<script>
var server = io.connect('http://localhost:8080'); server.on('question', function(question) {
insertQuestion(question);
}); server.on('answer', function(question, answer){
answerQuestion(question, answer);
}); //Don't worry about these methods, just assume
//they insert the correct html into the DOM
// var insertQuestion = function(question) {
// } // var answerQuestion = function(question, answer) {
// }
</script>

[Node.js]30. Level 6: Listen 'Question' from client, and then Answer the Question的更多相关文章

  1. [Node.js]29. Level 6: Socket.io: Setting up Socket.io server-side & Client socket.io setup

    Below we've already created an express server, but we want to start building a real-time Q&A mod ...

  2. [Node.js]33. Level 7: Persisting Questions

    Let's go back to our live-moderation app and add some persistence, first to the questions people ask ...

  3. Node.js中测试mysql的代码var client = mysql.createClient运行出错:TypeError: Object # has no method ‘createClient’

    今天在WebStorm下熟悉一个node.js的项目,配置环境时,手一抖,将mysql包从0.8升级到了2.1.1,结果再运行时就出错了. [Fri Mar 14 2014 17:05:49] 连接数 ...

  4. [Node.js]31. Level 7: Redis coming for Node.js, Simple Redis Commands

    Let's start practicing using the redis key-value store from our node application. First require the  ...

  5. [Node.js]24. Level 5: Express, Express routes

    Create an express route that responds to GET requests at the URL /tweets that responds with the file ...

  6. [Node.js]28. Level 5: Express Server

    Now let's create an express server which queries out for this search term and just returns the json. ...

  7. [Node.js]26. Level 5 : Route rendering

    Instead of just writing out the quote to the response, instead render the quote.ejs template, passin ...

  8. [Node.js]25. Level 5. Route params

    Create a route that responds to a GET request '/quotes/<name>', then use the param from the UR ...

  9. [Node.js]23. Level 4: Semantic versioning

    Update the versions on your dependencies to be a little more flexible, adding the ~ in front of your ...

随机推荐

  1. 【10.7校内测试】【队列滑窗】【2-sat】【贪心+栈二分+线段树(noip模拟好题)】【生日祭!】

    比较好想的一道题,直接用队列滑窗,因为扫一遍往队列里加东西时,改变的只有一个值,开桶储存好就行了! #include<bits/stdc++.h> using namespace std; ...

  2. 【BZOJ-1396&2865】识别子串&字符串识别 后缀自动机/后缀树组 + 线段树

    1396: 识别子串 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 312  Solved: 193[Submit][Status][Discuss] ...

  3. SGU 403 Scientific Problem

    403. Scientific Problem Time limit per test: 0.25 second(s)Memory limit: 65536 kilobytes input: stan ...

  4. BZOJ 1191: [HNOI2006]超级英雄Hero 匈牙利算法

    1191: [HNOI2006]超级英雄Hero Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  Solved: 2xx 题目连接 http:/ ...

  5. ROS知识(14)----局部避障的动态窗口算法(DWA)及其调试的方法

    Dynamic Window Approach(DWA)是重要的局部轨迹规划算法,ROS中使用了DWA算法获得了很好的局部路径规划的效果.具体的教程可参考官方的导航调试资料Navigation Tun ...

  6. hdu 4647 Another Graph Game,想到了就是水题了。。

    题目是给一个无向图,其中每个节点都有点权,边也有边权,然后就有2个小朋友开始做游戏了ALICE &BOB 游戏规定ALICE 先行动然后是BOB,然后依次轮流行动,行动时可以任意选取一个节点并 ...

  7. 手动为Android 4.x 手机加入�自己的根证书(CA 证书)

    首先看Android 4.x 系统的证书存放位置: AOSP Android系统中CA证书文件的位置在:/ system/etc/security/cacerts/一系列的以数字命名的.0文件 方法一 ...

  8. 用最简单的例子理解复合模式(Composite Pattern)

    在显示树形结构时,复合模式有很好的体现.本篇显示如下部门结构: 以上,有的节点包含子节点,有的节点部包含子节点.不管是什么节点,每个节点就代表一个部门. 首先设计一个关于部门的抽象基类. public ...

  9. 相比xib 使用代码编排view 的一个明显的好处就是可以更好地重复使用已有代码,减少代码冗余。

    相比xib 使用代码编排view 的一个明显的好处就是可以更好地重复使用已有代码,减少代码冗余.

  10. IOS中为tableViewCell增加右侧标记(选中或者更多)

    if ([self.selectWys containsObject:[self.initCitys objectAtIndex:indexPath.row]]) { tvCell.accessory ...