[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 moderation service and we've decided to use socket.io.
Require socket.io and make sure it listens for requests on the express app.
Also, print out a message to the console whenever a new socket.io client connects to the server.
app.js
var express = require('express');
var socket = require('socket.io');
var app = express.createServer();
var io = socket.listen(app);
io.sockets.on('connection', function(client){
console.log("Welcome...");
});
In our html file, load the socket.io.js script and then use io.connect to connect to socket.io on the server. Connect to the server at http://localhost:8080.
Tip: the socket.io.js path you should use is /socket.io/socket.io.js. Express knows to serve the socket.io client js for this path.
index.html
<script src="/socket.io/socket.io.js"></script>
<script>
// use the socket.io server to connect to localhost:8080 here
var server = io.connect('http://localhost:8080');
</script>
In our client below, listen for 'question' events from the server and call the insertQuestionfunction whenever the event fires. The insertQuestion function is already created for you, and it's placed in its own file. It expects exactly one argument - the question.
index.html
<script src="/socket.io/socket.io.js" />
<script src="/insertQuestion.js" /> <script>
var server = io.connect('http://localhost:8080'); // insert code here
server.on('question', function(data){
insertQuestion(data);
});
</script>
insertQuestion.js
var insertQuestion = function(question){
var newQuestion = document.createElement('li');
newQuestion.innerHTML = question;
var questions = document.getElementsByTagName('ul')[0];
return questions.appendChild(newQuestion);
}
When a question is submitted to our server, we want to broadcast it out to all the connected clients so they can have a chance to answer it.
In the server below, listen for 'question' events from clients and then emit the 'question' event on all the other clients connected, passing them the question data.
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 here
client.on('question', function(question){
//All client, so it is broadcast
client.broadcast.emit('question', question);
});
});
In our real-time Q&A app, we want to allow each client only 1 question at a time, but how do we enforce this rule?
We can use socket.io's ability to save data on the client, so whenever a question is asked, we first want to check the 'question_asked' value on the client. If it's not already set to true, broadcast the question and then go ahead and set the value to true.
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...");
client.on('question', function(question) {
client.get('question_asked', function(err, asked){
if(!asked){
client.broadcast.emit('question', question);
client.set('question_asked', true);
}
});
});
});
[Node.js]29. Level 6: Socket.io: Setting up Socket.io server-side & Client socket.io setup的更多相关文章
- [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 ...
- C Socket Programming for Linux with a Server and Client Example Code
Typically two processes communicate with each other on a single system through one of the following ...
- [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]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]23. Level 4: Semantic versioning
Update the versions on your dependencies to be a little more flexible, adding the ~ in front of your ...
- [Node.js]22. Level 4: Dependency
Add two dependencies to your package.json file, connect and underscore. You'll want to useconnect ve ...
- [Node.js]32. Level 7: Working with Lists -- Redis
As we saw in the video, redis can do more than just simple key-value pairs. We are going to be using ...
- [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. ...
随机推荐
- [BZOJ3595][SCOI2014]方伯伯的OJ(裂点Splay)
用一棵Splay按名次维护每个点,其中一个节点对应初始编号连续的一段区间,这样总节点数是$O(m)$的. 对每个编号记录这个点被Splay的那个节点维护,用std::map存储,只记录被修改的点. 每 ...
- vijos 1894 二分
题意:在 Ninian 的花园里,有许多琼花,环绕着中间的凉亭.有 N 片琼花,组成一个环.Ninian 想在凉亭中发动 [セチの祈り] , 需要划分出三个区域的琼花,为了平均,要最大化面积最小的区域 ...
- [Visual Studio] SOA服务框架搭建
1.服务框架搭建 2.服务模板创建 3.Nuget引用 4.客户端调用 任务点: 1.分析SOA 2.修改SOA架构名称以及关键字 3.使用Nuget添加引用 4.选择服务模板进行创建 5.尝试调用 ...
- redhat server 5.3内核升极2.6.18 升级到 3.5 装systemtap 原创
1. 在 LINUX 3.5源代码目录下执行 yum install ncurses-devel make menuconfig 2 打开内核跟踪事件,用于SYSTEMTAP跟踪 kern ...
- POJ 3041(最小点覆盖)
题意: 假如你如今正处在一个N*N的矩阵中,这个矩阵里面有K个障碍物,你拥有一把武器,一发弹药一次能消灭一行或一列的障碍物,求最小的弹药消灭所有障碍物 输入为: N K 接下来有K行,每行包括 ...
- Flex4+spring+hibernate+BlazeDS整合案例
http://wenku.baidu.com/link?url=7v5xAyD2nvChQRT60QewpvAASFHMZNvD0OxX11OASYvae8jbVNsr5I000TwwYOlChzq0 ...
- 免费的Bootstrap管理后台模板集合
Free Bootstrap Admin Templates for Designers 1. Admin Lite AdminLTE - 是一个完全响应式管理模板.基于Bootstrap3的框架.高 ...
- MVC为用户创建专属文件夹
假设需要为用户创建专属文件夹,文件夹名为用户名,并且需要根据用户类型在不同的文件夹下创建目标文件夹. 在F盘创建"Users"文件夹,在其中创建"Gold"文件 ...
- jvm执行流程
首先给一个简单的Java示例,源代码如下: public class Main { private static int size=1; public static void main(String ...
- 将数据处理逻辑集中到一处进行管理,逐步实现真正有效的 MVC 分层结构
将数据处理逻辑集中到一处进行管理,逐步实现真正有效的 MVC 分层结构.