Preparation:
Server-side deployment with nodejs and socket.io
Directly on the code, simply:
app.js :
//http class
//var http = require('http');
//var socketIO = require('socket.io');
//var app = http.createServer();
//var PORT = 8081;
//app.listen(PORT);
//var io = socketIO.listen(app);
//https class
var http = require('http');
/ / file operation class
var fs = require('fs');
/ / Read the key and signing certificate
var options = {
key: fs.readFileSync('keys/server.key'),
cert: fs.readFileSync('keys/server.crt')
}
//socket.io class
var socketIO = require('socket.io');
/ / Build http server
var apps = http.createServer(options);
//Listen port
var SSL_PORT = 8443;
apps.listen(SSL_PORT);
/ / Build Sock.io server
var io = socketIO.listen(apps);
//Socket connection listener
io.sockets.on('connection', function (socket) {
//socket closed
socket.on('disconnect', function(reason){
//room
// var room = Object.keys(socket.rooms)[1];
// //socket id
// var socketId = socket.id;
// var message = {};
// message.from = socketId;
// message.room = room;
// console.log('disconnect: ' + socketId + ' reason:' + reason +' message: ' + JSON.stringify(message));
// //Close the connection
// socket.leave(room);
// //Get the room
// var clientsInRoom = io.sockets.adapter.rooms[room];
// if (clientsInRoom) {
// var otherSocketIds = Object.keys(clientsInRoom.sockets);
// for (var i = 0; i < otherSocketIds.length; i++) {
// // Forward [exit] message to other clients
// var otherSocket = io.sockets.connected[otherSocketIds[i]];
// otherSocket.emit('exit', message);
// }
// }
var socketId = socket.id;
console.log('disconnect: ' + socketId + ' reason:' + reason );
var message = {};
message.from = socketId;
message.room = '';
socket.broadcast.emit('exit', message);
});
/** client->server signaling set*/
//[createAndJoinRoom] Create and join the Room [room]
socket.on('createAndJoinRoom', function (message) {
var room = message.room;
console.log('Received createAndJoinRoom:' + room);
/ / Determine if the room exists
var clientsInRoom = io.sockets.adapter.rooms[room];
var numClients = clientsInRoom ? Object.keys(clientsInRoom.sockets).length : 0;
console.log('Room ' + room + ' now has ' + numClients + ' client(s)');
if (clientsInRoom) {
console.log(Object.keys(clientsInRoom.sockets));
}
if (numClients === 0) {
/** room does not exist Create (socket.join)*/ if it does not exist
//Join and create a room
socket.join(room);
console.log('Client ID ' + socket.id + ' created room ' + room);
/ / Send [created] message to the client [id, room, peers]
var data = {};
//socket id
data.id = socket.id;
//room id
data.room = room;
//Other connections are empty
data.peers = [];
//send
socket.emit('created', data);
} else {
/** room exists */
//Send [joined] message to other clients in the room [id,room]
var data = {};
//socket id
data.id = socket.id;
//room id
data.room = room;
//Send other clients in the room
io.sockets.in(room).emit('joined', data);
/ / Send [created] message to the client [id, room, peers]
var data = {};
//socket id
data.id = socket.id;
//room id
data.room = room;
//Other connections
var peers = new Array();
var otherSocketIds = Object.keys(clientsInRoom.sockets);
console.log('Socket length ' + otherSocketIds.length);
for (var i = 0; i < otherSocketIds.length; i++) {
var peer = {};
peer.id = otherSocketIds[i];
peers.push(peer);
}
data.peers = peers;
//send
socket.emit('created', data);
//Join the room
socket.join(room);
console.log('Client ID ' + socket.id + ' joined room ' + room);
}
});
//[offer] forwards the offer message to the other clients in the room [from,to,room,sdp]
socket.on('offer', function (message) {
var room = Object.keys(socket.rooms)[1];
console.log('Received offer: ' + message.from + ' room:' + room + ' message: ' + JSON.stringify(message) );
/ / Forward [offer] message to other clients
/ / Find the corresponding connection according to the id
var otherClient = io.sockets.connected[message.to];
if (!otherClient) {
return;
}
otherClient.emit('offer', message);
});
//[answer] Forward answer message to room other clients [from,to,room,sdp]
socket.on('answer', function (message) {
var room = Object.keys(socket.rooms)[1];
console.log('Received answer: ' + message.from + ' room:' + room + ' message: ' + JSON.stringify(message));
/ / Forward [answer] message to other clients
/ / Find the corresponding connection according to the id
var otherClient = io.sockets.connected[message.to];
if (!otherClient) {
return;
}
otherClient.emit('answer', message);
});
//[candidate] forwards the candidate message to the other clients in the room [from,to,room,candidate[sdpMid,sdpMLineIndex,sdp]]
socket.on('candidate', function (message) {
console.log('Received candidate: ' + message.from + ' message: ' + JSON.stringify(message));
/ / Forward [candidate] message to other clients
/ / Find the corresponding connection according to the id
var otherClient = io.sockets.connected[message.to];
if (!otherClient) {
return;
}
otherClient.emit('candidate', message);
});
//[exit] close the connection and forward the exit message to the other clients in the room [from,room]
socket.on('exit', function (message) {
console.log('Received exit: ' + message.from + ' message: ' + JSON.stringify(message));
var room = message.room;
/ / Close the connection
socket.leave(room);
//Get the room
var clientsInRoom = io.sockets.adapter.rooms[room];
if (clientsInRoom) {
var otherSocketIds = Object.keys(clientsInRoom.sockets);
for (var i = 0; i < otherSocketIds.length; i++) {
/ / Forward [exit] message to other clients
var otherSocket = io.sockets.connected[otherSocketIds[i]];
otherSocket.emit('exit', message);
}
}
});
});
/** Build html page */
var express = require("express");
var htmlApp = express();
htmlApp.use(express.static("htmlTest")).listen(8080)
var httpsServer = http.createServer(options,htmlApp);
httpsServer.listen(8441);
// //http static route
htmlApp.use(express.static("htmlTest")).listen(8080);
Js script file
socket.io.js:
webRtc.html: webRtc.html
Two mobile phones open hotspots, under Firefox browser, to achieve functions

Demo download link:
Peer-to-peer chat is first based on multi-threaded network programming, ...
The Internet is the largest computer network. Since its birth, it has always existed in two different ways: centralized and distributed. Client/Server mode (C/S) is the most traditional and mature cen...
p2p communication method such as QQ transfer files, When an online user A sends and receives files to another online user B, the files can be transferred equally without going through the server; An o...
1. Create a server 2. Create a client 3. Run as shown in the figure: At the beginning, I was able to run. After prompting that the connection was successful, I reported java.lang.NullPointerException ...
(function(f){if(typeof exports===“object”&&typeof module!“undefined”){module.exports=f()}else if(typeof define=“function”&&define.amd){define([],f)}...
First select 2 PCs, two Loptops (notebook type), and two switches to construct the topology map Use a crossover cable between two devices of the same type, and use a straight-through cable between two...
🔞In order to have a private video chat with my little sister, I developed a peer-to-peer video chat room. WebRTC, whose name is derived from the abbreviation of Web Real-Time Communication (English: ...
1. Register a WebSocket in the background Using the sub-protocol STOMP of WebSocket 2. Custom database authentication method There are many authentication methods, you can use the memory storage user ...
MPI notes (a) environment MPI notes (2) peer-to-peer communication MPI notes (three) set communication MPI notes (4) data types and derived data types MPI notes (5) group and communication factors MPI...