[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 ...
随机推荐
- android 后台 activity 被系统回收 保存状态
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 活动被系统回收, 要保存状态 ,用到 活动的 在保存实例时候 的 这个方法. 当系统异常 ...
- [POI2015]Łasuchy
[POI2015]Łasuchy 题目大意: 圆桌上摆放着\(n(n\le10^6)\)份食物,围成一圈,第\(i\)份食物所含热量为\(c_i\). 相邻两份食物之间坐着一个人,共有\(n\)个人. ...
- 更好的浏览器动画实现 requestAnimationFrame
requestAnimationFrame 是专门为实现高性能的帧动画而设计的一个API: js一般是借助setTimeout或setInterval这两个函数实现动画,性能不佳. css3动画,性能 ...
- Codeforces Round #361 (Div. 2) D. Friends and Subsequences 二分
D. Friends and Subsequences 题目连接: http://www.codeforces.com/contest/689/problem/D Description Mike a ...
- Codeforces Round #358 (Div. 2) E. Alyona and Triangles 随机化
E. Alyona and Triangles 题目连接: http://codeforces.com/contest/682/problem/E Description You are given ...
- Codeforces Round #295 (Div. 2)B - Two Buttons BFS
B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- js面向对象写页面
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- MySQL的冷备份和热备份概念理解(转)
一.冷备份(off, 慢, 时间点上恢复) 冷备份发生在数据库已经正常关闭的情况下,当正常关闭时会提供给我们一个完整的数据库.冷备份是将关键性文件拷贝到另外位置的一种说法.对于备份数据库信息而言,冷备 ...
- Smali语法简单介绍
Smali语言其实就是Davlik的寄存器语言: Smali语言就是android的应用程序.apk通过apktool反编译出来的都有一个smali文件夹,里面都是以.smali结尾的文件,文件的展示 ...
- PostgreSQL 资料库
https://yq.aliyun.com/articles/59251 https://github.com/digoal/blog/blob/master/201609/20160929_02.m ...