HTML5 Web socket and socket. io

Source: Internet
Author: User
Tags emit

Principle: The client uses regular http to request webpage requests to execute javascript scripts and open a connection to server. when there is new information, the server and client can send information to each other (Real-time traffic from the server to the client and from the client to the server HTML5 WebSockets client Description: readyState: CONNECTING (0): indicates that no connection has been established; OPEN (1): A connection has been established for communication; CLOSING (2): CLOSING the handshake; CLOSED (3): the connection is CLOSED or cannot be opened. url: The network address of the WebSocket server. The protocol is generally "ws" or "wss (encrypted communication)". Event: send: the method for sending data close to the server is Close the connection; Establish an onopen connection, that is, the event triggered when the handshake is successful; the event triggered when the onmessage receives the server message; The event triggered by the onerror exception; use example: copy the code // create a Socket instance var socket = new WebSocket ('ws: // localhost: 100'); // open the Socket socket. onopen = function (event) {// send an initialization message socket. send ('I am the client and I \' m listening! '); // Listen to the message socket. onmessage = function (event) {console. log ('client received ed a message', event) ;}; // listen to the Socket closed socket. onclose = function (event) {console. log ('client notified socket has closed ', event) ;}; // close the Socket .... // socket. close ()}; copy the jWebSocket (Java) web-socket-ruby (ruby) Socket IO-node (node. js) the following uses socket. io description, Environment Description: (node. js Installation http://www.cnblogs.com/wishyouhappy/p/3647037.html ) 1. install socket. io npm install-g socket. io 2. use require to introduce http and socket. io var http = require ("http"); var io = require ('socket. io '); 3. create server var server = http. createServer (function (request, response) {response. writeHead (200, {"Content-Type": "text/html"}); response. write ("WebSocket Start ~~~~~~~~~~~~ "); Response. end ("");}). listen (8000); 4. listen to var socket = io. listen (server); 5. example: copy the code var http = require ("http"); var io = require ('socket. io '); var server = http. createServer (function (request, response) {response. writeHead (200, {"Content-Type": "text/html"}); response. write ("WebSocket Start ~~~~~~~~~~~~ "); Response. end ("");}). listen (8000); var socket = io. listen (server); // Add a connection listener socket. on ('connection', function (client) {client. on ('message', function (event) {console. log ('Received message from client! ', Event) ;}); client. on ('disconnect', function () {clearInterval (interval); console. log ('server has disconnected');}); copy the code data to send data in two ways: send and emit. Both send and emit can send data, however, emit can customize events as follows: emit: Copy code // server socket. on ('connection', function (client) {client. on ('message', function (event) {client. emit ('emitmessage', {hello: 'messgge received, wish you happy '+ new Date (). toString ()}) ;};}); // client socket. on ('emit Message ', function (data) {document. getElementById ("result "). innerHTML + = data. hello + "<br/>" ;}); copy the code send: copy the code // server socket. on ('connection', function (client) {client. send ('Hello, I am the Server') ;}); // client socket. on ('message', function (data) {document. getElementById ("result "). innerHTML + = data + "<br/>" ;}); copy the code instance: (socket. io) Client: copy the Code <! DOCTYPE html>

Related Article

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.