Home > Web Front-end > JS Tutorial > body text

Detailed explanation of node.js using websocket based on express

小云云
Release: 2017-12-23 09:03:50
Original
2779 people have browsed it

This article mainly introduces the method of node.js using websocket based on express. It analyzes the related settings and usage techniques of node.js based on express calling websocket based on the example. Friends who need it can refer to it. I hope it can help everyone.

I also searched for this effect for a long time, and the test was successful. Anyway, it was successful, let’s take a look.

First you need to install the socket.io module


npm install socket.io --save
Copy after login

Then open express's app.js and import the module. Add two lines

# below the


var app = express();
Copy after login

around line 12.


##

var server = require('http').Server(app);
var io = require('socket.io')(server);
Copy after login

Then add


app.use(function(req, res, next){
 res.io = io;
 next();
});
Copy after login

to more than 20 lines. Since I don’t like to start from www, so in app.js Added a few more lines of code to the second to last line at the bottom


var port = 3000;
app.set('port', port);
server.listen(port);
Copy after login

This will start listening to port 3000

In this way, the preparations are complete Completed, and then start the example

Use the official sample code to test, you can write it directly into app.js


io.on('connection', function (socket) {
 socket.emit('news', { hello: 'world1' });
 socket.on('my other event', function (data) {
  console.log(data);
 });
});
Copy after login

The preparation of the server has been completed , first introduce the socket.js file into the client page


<script src=&#39;javascripts/socket.io-1.4.5.js&#39;></script>
var socket = io.connect("//localhost:3000");
socket.on(&#39;news&#39;, function (data) {
  console.log(data);
  alert(data);
  socket.emit(&#39;my other event&#39;, { my: &#39;data&#39; });
});
Copy after login

Then open the page and test it

If you want to send information from the server to only one page , you can write like this

router.get(&#39;/&#39;, function(req, res, next) {
  //只有当前页面可以获得
  res.io.on(&#39;connection&#39;, function(socket) {
    socket.emit(&#39;news&#39;, {
      hello: &#39;world&#39;
    });
    socket.on(&#39;my other event&#39;, function(data) {
      console.log(data);
    });
  });
  //所有页面都可以获得
  var io = require("../app").io;
  io.emit("news",{hello:"myworld"});
  res.render("pclogin.ejs", {});
});
Copy after login
Related recommendations:

About the solution to the problem that the external network of the WebSocket deployment server cannot be connected

About websocket in php Detailed introduction

nodejs+websocket completes a chat system function

The above is the detailed content of Detailed explanation of node.js using websocket based on express. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!