Mobile phone-to-web communication via WebSocket

Source: Internet
Author: User

Mobile phone and web communication, using WebSocket to complete the link between the two.


WebSocket chooses Socket.io class to implement.


Server-side, using Nodejs, the code relies on Express and Socket.io.

First set up an HTTP server


var app = require (' Express ') ();

var server = require (' http '). Server (APP);

var socketio = require ('./v1/socketio ');

var db = require ("./db/db.js");


Server.listen (+);


App.get ('/', function (req, res) {

Res.sendfile (__dirname + '/index.html ');

});


Then use this server to create the Websoket service


var io = socketio (http);


var socketarr = [];


Io.on (' connection ', function (socket) {

Console.log (' A user connected ');


});


After successful Unicom, the client communication can be organized through the on and emit methods provided by Socket.io.


Mobile-integrated Socket.io, iOS and Android demos are as follows:

Ios:https://github.com/socketio/socket.io-client-swift

Android:https://github.com/socketio/socket.io-client-java


The Web page wants to integrate Socket.io, download js file from Socket.io homepage.

Socket.io Home: https://socket.io/


Embed in a Web page

<script type= "Text/javascript" src= "/src/socket.io.min.js" charset= "UTF-8" ></script>


Use the following code to connect to the WebSocket server

var socket = io (' ws://socket.liboapp.cn ');

Communicates with the mobile through the on and emit methods of the socket.

Mobile phone-to-web communication via WebSocket

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.