[Node.js]30. Level 6: Listen 'Question' from client, and then Answer the Question
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的更多相关文章
- [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 ...
- [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 ...
- 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] 连接数 ...
- [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 ...
- [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 ...
- [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. ...
- [Node.js]26. Level 5 : Route rendering
Instead of just writing out the quote to the response, instead render the quote.ejs template, passin ...
- [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 ...
- [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 ...
随机推荐
- [BZOJ4569][SCOI2016]萌萌哒(倍增+并查集)
首先有一个显然的$O(n^2)$暴力做法,将每个位置看成点,然后将所有限制相等的数之间用并查集合并,最后答案就是9*(10^连通块的个数).(特判n=1时就是10). 然后比较容易想到的是,由于每次合 ...
- poj 3630 Phone List 贪心
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23722 Accepted: 7289 Descr ...
- C#高级编程9-第3章 对象与类型
类与结构 类和结构都是对象的模板 类定义了处理和访问数据的方法,通过类的实例化进行逻辑处理 类与结构的区别是类是引用类型,存储在托管堆上:结构是值类型,存储在栈上的: 类使用class进行修饰,结构使 ...
- Qt 4.7.2移植到ARM教程
Qt的移植包括步骤如下: 1.下载并安装VMware WorkStation 12(最好比较高级的版本,早期的版本共享目录在虚拟机里可能显 示不了). 2.下载ubuntu 14.0.4(最好是lts ...
- 国产Linux滋生腐败
回想过去,2002年12月11日至12日,信息产业部与科技部联合主办"Linux软件与应用猜測研讨会".影响中国IT业的重要人士,包含政府决策者.学界权威.主要Linux推动厂商等 ...
- Emoji表情符号录入MySQL数据库报错的解决方案
原文:http://blog.itpub.net/26230597/viewspace-1243233/ 1,查看tomcat后台日志,核心报错信息如下: Caused by: java.sql.S ...
- ie不支持max-height的解决之法
.div{ max-height: 100px; _height:expression(this.scrollHeight > 100 ? "100px" : "a ...
- C#,数据类型扩展 z
MACD的公式 DIFF : EMA(CLOSE,SHORT) - EMA(CLOSE,LONG);DEA : EMA(DIFF,M); MACD : 2*(DIFF-DEA), COLORSTIC ...
- 《学习opencv》笔记——矩阵和图像操作——cvInRange,cvInRangeS,cvInvert and cvMahalonobis
矩阵和图像的操作 (1)cvInRange函数 其结构 void cvInRange(//提取图像中在阈值中间的部分 const CvArr* src,//目标图像 const CvArr* lowe ...
- java静态初始化数据
1.通过静态成员变量和静态方法组合(比较单一) public class A{ private static String t=getInit(); private static String get ...