Use socket.io to create a socket.io server. However, the 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.
The code copy is as follows:
var sio=require("scoket.io");
var socket=sio.listen(server);
socket is a socket.io server created on the server.
When the client establishes a connection with the server, the connection event of the socket.io service is triggered.
The code copy is as follows:
socket.on("connection",function(socket){
});
The socket parameter in the callback function is a socket port object that establishes a connection between the server and the client.
When a message sent by the client is received, a message event of the socket port object is issued.
The code copy is as follows:
socket.on("message",function(msg){
});
The parameters of the callback function are messages sent by the client.
You can use socket.send(msg) to send a message to the client.
The disconnect event is triggered when the server-side client-side connection is disconnected.
The code copy is as follows:
socket.on("disconnect",funciton(){
});
This callback function does not apply any parameters.
Server-side server.js code:
The code copy 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 connection establishment");
socket.send("Hello");
socket.on("message", function (msg) {
console.log("Received a message:"+msg);
});
socket.on("disconnect", function () {
console.log("Client disconnect.");
});
});
Create client index.html code:
The code copy is as follows:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket=io.connect();
socket.on("message", function (data) {
console.log(data);
socket.send("Message accepted.")
});
socket.on("disconnect", function () {
console.log("Server side disconnect.");
});
</script>
</head>
<body>
</body>
</html>
This code: /socket.io/socket.io.js is provided with the server-side socket.io class library, and there is no need to actually place a socket.io.js file on the client.
In the script file, first use the io.connect() method to connect to the server socket.io server.
This method returns a client socket port object that establishes a connection to the server.
When a message is received from the server, the message event of the client socket port object is triggered.
The code copy is as follows:
socket.on("message",function(msg){
});
msg is data sent by the server;
You can also use the send() method of the client socket object to send data to the server.
The code copy is as follows:
socket.send(msg);
When the server disconnects, the disconnect event of the client socket port object is triggered.
The code copy is as follows:
socket.on("disconnect",function(){
})
This callback function does not use any parameters.
Notice:
The client's message mechanism is exactly the same as the server-side message processing mechanism. Because socket.io ensures that the client and the server share the same API.
Results after running:
When the browser is closed, the connection to the server is disconnected. At this time, the server triggers the disconnect event and the client disconnects.