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. 20162327WJH使用队列:模拟票务站台代码分析

    20162327WJH使用队列:模拟票务站台代码分析 用链队实现队列的情况 1.用链表实现队列的代码 关键方法代码及补全代(LinkedOueue类) public void enqueue(T el ...

  2. dll文件反编译,c#、vb动态库反编译

    最近开发遇到一个项目,对方提供一个c#编写的动态库,图片处理需要调用该动态库方法,发现一张图片处理起来需要5s时间,对方无法提供有效解决手段,抱着试一试的想法反编译的对方的动态库,发现其中问题. 一下 ...

  3. Codeforces Round #298 (Div. 2) D. Handshakes 构造

    D. Handshakes Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/problem ...

  4. Codecademy python

    #1 print "Welcome to Python!" #2 my_variable = #3 # Set the variables to the values listed ...

  5. cocos2d-x3.0 Slider

    .h #include "cocos2d.h" #include "cocos-ext.h" #include "ui/CocosGUI.h" ...

  6. mybatis逆向工程--自动生成实体代码(mybatis-generator)

    随便找个目录,  添加文件, 如图 主要是两个jar包,  generator的下载路径:   https://github.com/mybatis/generator/releases   驱动包随 ...

  7. HDU5087 Revenge of LIS II (LIS变形)

    题目链接:pid=5087">http://acm.hdu.edu.cn/showproblem.php?pid=5087 题意: 求第二长的最长递增序列的长度 分析: 用step[i ...

  8. waitdialogform z

    namespace DevExpress.Utils { using DevExpress.LookAndFeel; using DevExpress.Skins; using DevExpress. ...

  9. SVN环境

    SVN服务器搭建和使用(一) Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上 ...

  10. activity之间參数传递&amp;&amp;获取activity返回值&amp;&amp;activity生命周期

    Activity之间參数传递 A activity想将參数传给B activity时能够利用Intent将消息带过去 Intent intent = new Intent(this,BActivity ...