Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat small program multi-person audio and video dialogue


May 18, 2021 WeChat Mini Program Development Document


Table of contents


Multiplayer audio and video conversations

The ability to implement multiplayer audio and video conversations in small programs.

Application opens

Small program management background, "development" - "interface settings" in the self-service to open the component permissions. Related interface wx.joinVoIPChat and component voip-room.

Call the process

Developers only need to provide a unique identification of the room to join the designated room. I ncoming users with the same unique identity will enter the same room. T o ensure that the front-end incoming groupId is trusted, the wx.joinVoIPChat interface requires an incoming signature. S ee the signature algorithm below. When you join a video room, you can display a member screen in conjunction with the voip-room component.

Front-end interface

The signature algorithm

There are four parameters that need to be passed in to generate a signature:

The name of the argument Description
appId AppId for games
groupId The unique logo of the game room is guaranteed by the game itself
nonceStr Random string, length should be less than 128
timeStamp Generate this random string UNIX timestamp (accurate to second)

The signature algorithm is:

signature = hmac_sha256([appId, groupId, nonceStr, timeStamp].sort().join(''), sessionKey)

Specifically, this algorithm is divided into several steps:

  1. The four values of Appid Groupid Noncestr TimeStamp are represented in the form of string, sorted by words;
  2. Stitch the four strings of the row order together;
  3. Using session_key as key, the result string hmac_sha256 2 is calculated using the hmac_sha256 algorithm, and the result is signature

Example:

appId = 'wx20afc706a711eefc'
groupId = '1559129713_672975982'
nonceStr = '8AP6DT9ybtniUJfb'
timeStamp = '1559129714'
session_key = 'gDyVgzwa0mFz9uUP7M6GQQ=='

str = [appId, groupId, nonceStr, timeStamp].sort().join('') = '1559129713_67297598215591297148AP6DT9ybtniUJfbwx20afc706a711eefc'
signature = hmac_sha256('1559129713_67297598215591297148AP6DT9ybtniUJfbwx20afc706a711eefc', sessionKey) = 'b002b824688dd8593a6079e11d8c5e8734fbcb39a6d5906eb347bfbcad79c617'

Complete the signature with cloud development

In cloud development, you can't get session_key, but a separate function, cloud.getVoIPSign, is provided to calculate the signature.

const cloud = require('wx-server-sdk')
cloud.init()

exports.main = async (event, context) => {
  const signature = cloud.getVoIPSign({
    groupId: 'xxx',
    timestamp: 123,
    nonce: 'yyy'
  })
  return signature
}

Limit the number of people

A maximum of 10 people can be added to each room at the same time.

Frequency limit

For each small program, up to 100,000 rooms per day are allowed to be created. W hen everyone exits the room, the room is destroyed. At this point, if you rejoin the room with a previously used groupId, it will be calculated as opening a new room.