[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. ...
随机推荐
- [COGS 2066]七十与十七
http://218.28.19.228/cogs/problem/problem.php?pid=2066 [题目描述] 七十君最近爱上了排序算法,于是Ta让十七君给Ta讲冒泡排序. 十七君给七十君 ...
- 洛谷P3119 USACO15JAN 草鉴定
题目描述 In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-w ...
- python开发_tkinter_多级子菜单
在之前的blog中有提到python的tkinter中的菜单操作 python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐 python开发_tkinter_窗口控件_自 ...
- python开发_platform_获取操作系统详细信息工具
''' python中,platform模块给我们提供了很多方法去获取操作系统的信息 如: import platform platform.platform() #获取操作系统名称及版本号,'Win ...
- webpack vuejs 和 vue-router 如何使用?
读本文之前,建议对webpack和vuejs有初步的了解,通过webpack的官网和vuejs的中文官网了解即可 网站主要目录://某些文件不一定全部罗列出来,注意观察 vue-wepack -src ...
- Backup your Android without root or custom recovery -- adb backup
ecently discovered a neat new way to back up apps on my Android without having to use Titanium Backu ...
- Android设备运用Clockworkmod Recovery恢复模式安装定制的Rom
Clockworkmod Recovery是一个由Cyanogen团队开发的用于Android设备的第三方定制Recovery恢复模式,也称为CWM Recovery,具体它有什么用处呢?请看关于Go ...
- 【Go入门教程5】流程(if、goto、for、switch)和函数(多个返回值、变参、传值与传指针、defer、函数作为值/类型、Panic和Recover、main函数和init函数、import)
这小节我们要介绍Go里面的流程控制以及函数操作. 流程控制 流程控制在编程语言中是最伟大的发明了,因为有了它,你可以通过很简单的流程描述来表达很复杂的逻辑.Go中流程控制分三大类:条件判断,循环控制和 ...
- JavaScript 新手的踩坑日记
引语 在1995年5月,Eich 大神在10天内就写出了第一个脚本语言的版本,JavaScript 的第一个代号是 Mocha,Marc Andreesen 起的这个名字.由于商标问题以及很多产品已经 ...
- java基础学习总结——方法的重载(overload)
一.方法的重载 方法名一样,但参数不一样,这就是重载(overload). 所谓的参数不一样,主要有两点:第一是参数的个数不一样,第二是参数的类型不一样.只要这两方面有其中的一方面不一样就可以构成方法 ...