Use Socket.io to create a Socket.io server. However, the server relies on an HTTP server that has already been created.
After the HTTP server is running, use the Listen method to attach a Socket.io server to the HTTP server.
Copy Code code as follows:
var sio=require ("Scoket.io");
var socket=sio.listen (server);
A socket is a Socket.io server that is created on the server basis.
The connection event that triggers the Socket.io service when the client establishes a connection to the server side.
Copy Code code as follows:
Socket.on ("Connection", function (socket) {
});
The socket parameter in the callback function is the socket port object that the server-side establishes a connection to the client.
The message event for the socket port object is emitted when messages sent by the client are received.
Copy Code code as follows:
Socket.on ("Message", function (msg) {
});
The parameter of the callback function is the message sent by the client.
You can send a message to the client using Socket.send (msg).
Triggers the Disconnect event when a server-side connection to a client is disconnected.
Copy Code code as follows:
Socket.on ("Disconnect", Funciton () {
});
The callback function does not work with any arguments.
Server-side Server.js code:
Copy Code code 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.");
});
});
To create the client index.html code:
Copy Code code as follows:
<! DOCTYPE html>
<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 to.")
});
Socket.on ("Disconnect", function () {
Console.log ("server-side disconnected.");
});
</script>
<body>
</body>
This code:/socket.io/socket.io.js has server-side Socket.io class library provided, do not need the client to actually place a socket.io.js file.
In the script file, first connect the server-side Socket.io server using the Io.connect () method.
This method returns a client socket port object that establishes a connection to the server side.
The message event that triggers the client socket port object when receiving messages sent to the server side.
Copy Code code as follows:
Socket.on ("Message", function (msg) {
});
MSG is the server-side data sent;
You can also send data to the server side using the Send () method of the client's socket object.
Copy Code code as follows:
Triggers the disconnect event of the client socket port object when the server side disconnects.
Copy Code code as follows:
Socket.on ("Disconnect", function () {
})
This callback function does not use any arguments.
Attention:
The client's message mechanism is exactly the same as the server-side message processing mechanism. Because Socket.io ensures that the client shares the same API as the server side.
Results after run:
When the browser is turned off, the connection to the server is disconnected, at which point the server side triggers the Disconnect event and the client disconnects.