@100mslive/hmsvideo-web 中文文档教程
hmsvideo-sdk
用于 100mslive 后端的 Web SDK。
Installation
npm install @100mslive/hmsvideo-web
Usage
```ts import { HMSPeer, HMSClientConfig, HMSClient, HMSMediaStreamConstraints } from "@100mslive/hmsvideo-web';
const peer = new HMSPeer(userName:"
// 设置处理程序 client.on('connect',() => { // 这是我们可以调用 join(room)
的地方 }); client.on('断开连接', () => {}); client.on('peer-join', (room, peer) => { // 在 UI 中显示通知或 toast 消息 }); client.on('peer-leave', (room, peer) => { // 在 UI 中显示通知或 toast 消息 }); client.on('stream-add', (room, peer, streamInfo) => { // 如果需要,订阅流 }); client.on('stream-remove', (room, peer, streamInfo) => { // 如果需要,删除远程流 }); client.on('broadcast', (room, peer,message) => { // 显示通知或更新聊天 UI });
// 连接 尝试 { 等待 client.connect() } 抓住(错误){ // 处理错误 }
// 使用唯一的房间 ID 加入房间 常量房间 =
// 获取本地流 常量约束 = 新的 HMSMediaStreamConstraints() 尝试 { const localStream = await client.getLocalStream(constraints: HMSMediaStreamConstraints); } 抓住(错误){ // 处理错误 }
// 发布本地流 尝试 { 等待 client.publish(localStream, room); } 抓住(错误){ // 处理错误 }
// 取消发布本地流 尝试 { await client.unpublish(localStream, room); } 抓住(错误){ // 处理错误 }
// 订阅远程流 尝试 { const remoteStream = await client.subscribe(mid, roomId); // 用 remoteStream 做点什么 } 抓住(错误){ // 处理错误 }
// 取消订阅流 尝试 { await client.unsubscribe(remoteStream, roomId); } 抓住(错误){ // 处理错误 }
// 向房间广播有效负载 尝试 { await client.broadcast(payload, roomId); } 抓住(错误){ // 处理错误 }
// 关闭客户端连接 客户端断开连接();
hmsvideo-sdk
Web SDK for the 100mslive backend.
Installation
npm install @100mslive/hmsvideo-web
Usage
```ts import { HMSPeer, HMSClientConfig, HMSClient, HMSMediaStreamConstraints } from "@100mslive/hmsvideo-web';
const peer = new HMSPeer(userName:"
// Setup handlers client.on('connect',() => { // This is where we can call join(room)
}); client.on('disconnect', () => {}); client.on('peer-join', (room, peer) => { // Show a notification or toast message in the UI }); client.on('peer-leave', (room, peer) => { // Show a notification or toast message in the UI }); client.on('stream-add', (room, peer, streamInfo) => { // subscribe to the stream if needed }); client.on('stream-remove', (room, peer, streamInfo) => { // Remove remote stream if needed }); client.on('broadcast', (room, peer ,message) => { // Show a notification or update chat UI });
// Connect try { await client.connect() } catch(err) { // Handle error }
// Join a room using a unique room id const room =
// Get a local stream const constraints = new HMSMediaStreamConstraints() try { const localStream = await client.getLocalStream(constraints: HMSMediaStreamConstraints); } catch(err) { // Handle error }
// Publish local stream try { await client.publish(localStream, room); } catch(err) { // handle the error }
// Unpublish local stream try { await client.unpublish(localStream, room); } catch(err) { // handle error }
// Subscribe to a remote stream try { const remoteStream = await client.subscribe(mid, roomId); // Do something with remoteStream } catch(err) { // Handle error }
// Unsubscribe from a stream try { await client.unsubscribe(remoteStream, roomId); } catch(err) { // Handle error }
// Broadcast a payload to the room try { await client.broadcast(payload, roomId); } catch(err) { // Handle error }
// Close client connection client.disconnect();