Home > Web Front-end > JS Tutorial > Methods of using socket.io in node.js_node.js

Methods of using socket.io in node.js_node.js

WBOY
Release: 2016-05-16 16:26:56
Original
1468 people have browsed it

Use socket.io to create a socket.io server. But this server depends on an already created http server.

After the http server is running, use the listen method to attach a socket.io server to the http server.

Copy code The code is as follows:

var sio=require("scoket.io");
var socket=sio.listen(server);

Socket is a socket.io server created based on server.

When the client establishes a connection with the server, the connection event of the socket.io service is triggered.

Copy code The code is as follows:

socket.on("connection",function(socket){
});

The socket parameter in the callback function is the socket port object used to establish a connection between the server and the client.

When a message sent by the client is received, the message event of the socket port object is emitted.

Copy code The code is as follows:

socket.on("message",function(msg){
});

The parameter of the callback function is the message sent by the client.

You can use socket.send(msg) to send a message to the client.

The disconnect event is triggered when the connection between the server and the client is disconnected.

Copy code The code is as follows:

socket.on("disconnect", funciton(){
});

This callback function does not take any parameters.

Server-side server.js code:

Copy code The code is as follows:

var http=require("http");
var sio=require("socket.io");
var fs=require("fs");
var server=http.createServer(function (req,res) {
res.writeHead(200,{"Content-type":"text/html"});
res.end(fs.readFileSync("./index.html"));
});
server.listen(1337);
var socket=sio.listen(server);
socket.on("connection", function (socket) {
console.log("Client establishes connection");
​​socket.send("Hello");
socket.on("message", function (msg) {
console.log("Received a message:" msg);
});
​​socket.on("disconnect", function () {
console.log("Client disconnected.");
});
});

Create client index.html code:

Copy code The code is as follows:







<script><br>         var socket=io.connect();<br> socket.on("message", function (data) {<br> console.log(data);<br> socket.send("Message received.")<br>           });<br>            socket.on("disconnect", function () {<br> console.log("Server side disconnected.");<br>           });<br> </script>




This code:/socket.io/socket.io.js is provided by the server-side socket.io class library, and there is no need to actually place a socket.io.js file on the client side.

In the script file, first use the io.connect() method to connect to the server-side socket.io server.

This method returns a client socket port object that establishes a connection with the server.

When a message sent by the server is received, the message event of the client socket port object is triggered.

Copy code The code is as follows:

socket.on("message",function(msg){
});

msg is the data sent by the server;

You can also use the send() method of the client's socket object to send data to the server.

Copy code The code is as follows:

socket.send(msg);

When the server disconnects, the disconnect event of the client socket port object is triggered,

Copy code The code is as follows:

socket.on("disconnect",function(){
})

This callback function does not use any parameters.

Note:

The client's message mechanism is completely consistent with the server's message processing mechanism. Because socket.io ensures that the client and server share the same API.

Result after running:

When the browser is closed, the connection with the server is disconnected. At this time, the server triggers the disconnect event and the client disconnects.

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