The online example of WEBRTC is mostly code, the following is an example of a WEBRTC-to-one sample of code simplification, which is tested under Chrome 37. Where iceserver can be omitted, there is no iceserver when the same LAN can still communicate.
Client code:
When WEBRTC is implemented, the signaling server is required to facilitate communication between clients.
Here, you use node. JS's WS module to implement a WebSocket service as a signaling server. The Express module is also used to provide access to the HTML page.
The Server.js code is as follows:
var express = require (' Express '), App = Express (), Server = require (' http '). Createserver (app); Server.listen (3000); App.get ('/', function (req, res) {res.sendfile (__dirname + '/webrtc.html ');}); var websocketserver = require (' ws '). SERVER,WSS = new Websocketserver ({server:server});//store socket array, there can only be 2 sockets, each test requires a reboot, otherwise the error var WSC = [],index = 1;//has Socket attached to Wss.on (' Connection ', function (WS) {console.log (' connection '); The socket is stored in array wsc.push (WS); Make a note of the other socket in the array subscript, because this test program only allows 2 sockets//So the first socket to deposit 0, the second is connected to deposit 1//Otherindex on the reverse, the first socket otherindex subscript 1, the second socket's otherindex subscript is 0 var otherindex = index--, desc = null; if (Otherindex = = 1) {desc = ' first socket '; } else {desc = ' second socket '; }//forwards the received message Ws.on (' message ', function (message) {var json = json.parse (message); Console.log (' Received (' + desc + '): ', JSON); Wsc[otherindex].send (Message, function (error) {if (error) { Console.log (' Send message ' ERROR (' + desc + '): ', error); } }); });});
Use the node server.js to start the service after using NPM to install the required modules.
Use Chrome when testing:
First browser window access page: http://127.0.0.1:3000, allows the use of the camera and microphone in the popup prompts.
Second browser window access page: Http://127.0.0.1:3000#true, #true表示它是一个发起方, also allows the use of the camera and microphone in the popup prompts.
At this point the page should be able to see 2 images, one is local, the other is far-end.
You can access this page for real-time communication in 2 different locations by deploying the IP in your code to the outside network.
Subscription Number:
Source Text Address: http://blog.gopersist.com/2014/10/21/webrtc-simple/
The simplest example of WEBRTC