Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chatroom-web-sdk

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chatroom-web-sdk

```sh npm install -S chatroom-web ```

  • 1.0.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

chatroom-web

安装

npm install -S chatroom-web

如何使用

import * as uuid from "uuid";
// 引用 SDK
import { SimpleSDK } from "chatroom-sdk/SimpleSDK";
// 引用麦位管理
import { SeatManager } from 'chatroom-sdk/SetaManager';
// 引用麦位类型及常数
import * as seatTypes from 'chatroom-sdk/SeatTypes';

// SDK, config 配置
const config = {
    appId = 0,       // 申请账号的 app id
    signKey = "",    // 申请账号的 sign key
    isTestEnv = true // 是否使用测试环境
};
const userId = uuid.v4();
const userId2 = uuid.v4();
const token = "";

// SDK, 构建新 SDK 对象
const simpleSDK = new SimpleSDK({ appId: config.appId, isTestEnv: config.isTestEnv });
// 麦位, 构建新麦位管理对象
const manager = new SeatManager({ userId: userId });

// SDK, 设置用户名
simpleSDK.setConfig({ idName: userId });

// 麦位, 设置麦位状态更新回调
manager.onSeatsCenterChange = (seatsCenter, isRemote) => {
    if (isRemote) {
        console.log("update from remote");
    } else {
        console.log("update from local");
    }
    // 麦位, 美化麦位状态, 方便调试和查看
    const res = manager.beautiful(seatsCenter);
    console.log(res);

    // 麦位, 获取当前用户麦位状态
    const currSeatStatus = manager.getCurrSeatStatus();
    console.log(currSeatStatus);

    if (!isRemote) {
        const content = JSON.stringify(seatsCenter);
        // 麦位, 当本地更新时, 用可靠消息发送新的麦位状态
        return simpleSDK.sendReliableMessage(manager.messageType, JSON.stringify(content));
    } else {
        // 麦位, 返回空 resolve 至回调
        return Promise.resolve(0);
    }
}

// SDK, 添加可靠消息回调
simpleSDK.addOnRecvReliableMessage((type, seq, data) => {
    if (!data) return;
    const seatsCenter = JSON.parse(data as string);
    if (!seatsCenter[seatTypes.SEATS_Status_KEY]) return;
    manager.updateSeatsCenter(seatsCenter);
});
// SDK, 登录房间
await simpleSDK.login({ roomId, token });

const useSeatControls = async () => {
    // 上麦
    await manager.takeSeat(1);
    // 下麦
    await manager.leaveSeat();
    console.log(manager.getCurrSeatStatus());

    // 锁麦
    await manager.lockSeat(0);
    // 解锁麦位
    await manager.unlockSeat(0);

    // 关闭麦位
    await manager.closeSeat(0);
    // 打开麦位
    await manager.openSeat(0);

    // 静音麦位
    await manager.muteSeat(0);
    // 取消静音
    await manager.unmuteSeat(0);

    // 静音所有麦位
    await manager.muteAllSeat();
    // 取消所有麦位静音
    await manager.unmuteAllSeat();

    // 抱人上麦
    await manager.pickupSeat({ [seatTypes.USER_ID_KEY]: userId2, [seatTypes.USER_NAME_KEY]: userId2 }, 2);
    // 抱人下麦
    await manager.pickupDownSeat({ [seatTypes.USER_ID_KEY]: userId2, [seatTypes.USER_NAME_KEY]: userId2 }, 2);
};

useSeatControls();

WebRTC SDK 方法的使用

  • SimpleSDK 内置了一些常用方法
    • login
    • setConfig
    • getRoomList
    • startPreview
    • stopPreview
    • startPublish
    • stopPublish
    • playStream
    • playStreams
    • stopPlayStream
    • stopPlayStreams
    • sendReliableMessage
    • addOnRecvReliableMessage
  • 其他 SDK 方法可通过 simpleSDK.client 实例获取,具体可参考文档 https://doc-zh.zego.im/zh/1806.html

注意事项

  • 当没用户在房间的时候,房间将会销毁,麦位状态将会重置
  • 必须已经登录才可发送消息
  • 移动端的播放麦位,必须渲染在页面上,并且触发点击才可自动播放
  • 麦位管理只提供麦位状态和麦位操作,具体业务推拉流逻辑需要自行添加开发

FAQs

Package last updated on 16 Jun 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc