The basic use method of node's Socket.io.

Source: Internet
Author: User

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 an Socket.io server to the HTTP server.

var sio=require ("Scoket.io");

var socket=sio.listen (server);

The socket is a Socket.io server created on the server basis.

The connection event that triggers the Socket.io service when the client establishes a connection to the server side.

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 of the socket port object is emitted when messages sent by the client are received.

Socket.on ("Message", function (msg) {

});

The parameters of the callback function are the messages sent by the client.

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

The disconnect event is triggered when a server-side client connection disconnects.

Socket.on ("Disconnect", Funciton () {

});

The callback function does not use any parameters.

Server-side Server.js code:

1 varHttp=require ("http");2 varSio=require ("Socket.io");3 varFs=require ("FS");4 varServer=http.createserver (function(req,res) {5Res.writehead (200,{"Content-type": "Text/html"});6Res.end (Fs.readfilesync ("./index.html"));7 });8Server.listen (1337);9 varsocket=Sio.listen (server);TenSocket.on ("Connection",function(socket) { OneConsole.log ("Client Establish connection"); ASocket.send ("Hello"); -Socket.on ("message",function(msg) { -Console.log ("received a message:" +msg); the     }); -Socket.on ("Disconnect",function () { -Console.log ("Client disconnects.")); -     }); +});

To create the client index.html code:

1<! DOCTYPE html>234<meta charset= "UTF-8" >5<title></title>6<script src= "/socket.io/socket.io.js" ></script>7<script>8         varsocket=Io.connect ();9Socket.on ("message",function(data) {Ten console.log (data); OneSocket.send ("The message has been accepted.")) A         }); -Socket.on ("Disconnect",function () { -Console.log ("Server-side disconnect.")); the         }); -</script> - -<body> +  -</body> +

This code:/socket.io/socket.io.js has a server-side Socket.io class library that does not require the client to actually place a socket.io.js file.

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 to the server side.

The message event that triggers the client socket port object when receiving messages sent to the server side.

Socket.on ("Message", function (msg) {

});

MSG is the server-side data sent;

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

Socket.send (msg);

The disconnect event that triggers the client socket port object when the server side disconnects,

Socket.on ("Disconnect", function () {

})

This callback function does not use any parameters.

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 with the server side.

Results after the run:

When the browser is closed, the connection to the server is disconnected, at which point the server-side triggers the disconnect event and the client disconnects.

The basic use method of node's Socket.io.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.