JAVA使用腾讯云实时音视频监听服务端事件回调记录

环境:java、腾讯云实时音视频
腾讯云实时音视频监听服务端事件回调文档:https://cloud.tencent.com/document/product/647/51586#event_type
1、设置回调秘钥、回调地址
JAVA使用腾讯云实时音视频监听服务端事件回调记录_第1张图片
2、编写回调方法、计算签名
JAVA使用腾讯云实时音视频监听服务端事件回调记录_第2张图片

下面展示一些 内联代码片

	/**
	 * 视频连线房间回调
	 * @param headers    事件回调消息的 header
	 * @param jsonObject 事件回调消息
	 * @return
	 */
	@ApiOperation("视频连线房间回调")
	@PostMapping("/roomCallback")
	public R roomCallback(HttpServletRequest request, @RequestHeader Map<String, String> headers, @RequestBody JSONObject jsonObject) throws Exception {
		//签名值
		String sign = headers.get("sign");
		System.out.println("腾讯云签名值:" + sign);
		RepeatedlyRequestWrapper repeatedlyRequest = (RepeatedlyRequestWrapper) request;
		String body = HttpHelper.getBodyString(repeatedlyRequest);
		body = body.substring(0, body.length() - 1);
		System.out.println("原始body:" + body);
		String secret = "你的回调秘钥";
		Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
		SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
		sha256_HMAC.init(secret_key);
		String hash1 = org.apache.commons.codec.binary.Base64.encodeBase64String(sha256_HMAC.doFinal(body.getBytes()));
		System.out.println("本地的签名值:" + hash1 + "----对比结果:" + sign.equals(hash1));
		System.out.println();
		return R.ok();
	}

获取请求body

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

/**
 * 通用http工具封装
 * 
 * @author xcz
 */
public class HttpHelper
{
    private static final Logger LOGGER = LoggerFactory.getLogger(HttpHelper.class);

    public static String getBodyString(ServletRequest request)
    {
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = null;
        try (InputStream inputStream = request.getInputStream())
        {
            reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
            String line = "";
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
        }
        catch (IOException e)
        {
            LOGGER.warn("getBodyString出现问题!");
        }
        finally
        {
            if (reader != null)
            {
                try
                {
                    reader.close();
                }
                catch (IOException e)
                {
                    LOGGER.error(ExceptionUtils.getMessage(e));
                }
            }
        }
        return sb.toString();
    }
}

结果:
JAVA使用腾讯云实时音视频监听服务端事件回调记录_第3张图片

你可能感兴趣的:(java,腾讯云,实时音视频)