Construction of WebRTC signaling server

In the introduction to WebRTC, it is mentioned that the signaling server is used to transmit data to the middle end, and the signaling server is an important role in realizing the communication between two webRTC middle ends. Let's implement the signaling server today.

As a front-end developer, I am not very familiar with back-end things, so I can only use some off-the-shelf server software and nodejs to build a signaling server.

Business logic

When two users want to communicate, they must first create a room, and after successfully joining the room, both parties can exchange necessary information.

When the two parties in the communication end the call, the user needs to send a message to the signaling server to leave the room. At this time, the signaling server needs to clear everyone in the room; if there is no one in the room, the empty room needs to be destroyed.

Such logic socket.io has already realized it for us, we only need to use it, and we don’t need to redevelop it ourselves.

Therefore, we use nodejs+express+socket.io to implement the signaling server.

  1. create server
const http = require('http');//引入http库
const express = require('express'); //引入express库

//创建HTTP服务,并侦听8980端口
const app = express();
const http_server = http.createServer(app);
http_server.listen(8080, '0.0.0.0');

First, create a web application through express; then call the createServer() method of the HTTP library to create an HTTP object, namely http_server; finally call the listen() method of the http_server object to listen on port 8080. Through the above steps, an HTTP service is realized.

  1. Register the callback function of socket.io
io.sockets.on('connection', (socket) => {
    
    

  //收到message时,进行转发
  socket.on('message', (message) => {
    
    
    //给另一端转发消息
    socket.to(room).emit('message', message);

  });

  //收到 join 消息
  socket.on('join', (room) => {
    
    
    var o = io.sockets.adapter.rooms[room];
    //得到房间里的人数
    var nc = o ? Object.keys(o.sockets).length : 0;
    if (nc < 2) {
    
     //如 果 房 间中 没 有 超 过 2 人
      socket.join(room);
      //发 送 joined消 息
      socket.emit('joined', room);
    } else {
    
     // max two clients
      socket.emit('full', room); //发 送 full 消 息
    }
  });
})

All messages are processed after the client establishes a connection with the server. Therefore, the processing function of the connection message needs to be registered in socket.io in advance.

   io.sockets.on('connection', (socket) => {
    
    
   	//收到message时,进行转发
  	socket.on('message', (message) => {
    
    
  
   		//给另一端转发消息
  		socket.to(room).emit('message', message);
  	});
  
  	//收到 join 消息
   	socket.on('join', (room) => {
    
    
  	var o = io.sockets.adapter.rooms[room];
  
  	//得到房间里的人数
  	var nc = o ? Object.keys(o.sockets).length : 0;
  	if (nc < 2){
    
     //如 果 房 间中 没 有 超 过 2 人
  		socket.join(room);
  
 		//发 送 joined消 息
  		socket.emit('joined', room);
  	} else {
    
     // max two clients
 		socket.emit('full', room); //发 送 full 消 息
   	}
  }
 });

If the server receives the message message, it does not do any processing and forwards it directly.
If it is a join message, first add the user to the room managed by the server, and then return a joined message to the client.
If there are already two users in the room when the user joins, reject the user's joining and return a full message to inform that the room is full. If there are other needs, you can monitor other events and operate by yourself.

Guess you like

Origin blog.csdn.net/qq_40850839/article/details/127972514