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