[WebService] Tokbox, OpenTok API 筆記
- Tokbox Developer Center @ Official Website
- OpenTok.js reference @ Official Website
Session
串流相關
session.connect(token, completionHandler)
session.disconnect()
session.subscribe(stream, targetElement, properties, completionHandler) -> subscriber
session.unsubscribe(subscriber)
session.publish(publisher, completionHandler)
session.unpublish(publisher)
資訊取得與傳遞:
session.getSubscribersForStream(stream)
session.getPublisherForStream(stream)
session.signal(signal, completionHandler)
事件相關
session.on(type, handler, context)
session.off(type, handler, context)
可用事件
archiveStarted
,archiveStopped
connectionCreated
,connectionDestroyed
sessionConnected
,sessionDisconnected
(見備註)sessionReconnected
,sessionReconnecting
streamCreated
,streamDestroyed
streamPropertyChanged
signal
,signal:type
⚠️
sessionConnected
和sessionDisconnected
事件中的 SessionConnectEvent 已經 deprecated,因此只可用來確認事件觸發的時間點,但在 SessionConnectEvent 中得不到太多資訊。
session.on('connectionCreated', handleConnectionCreated); // 自己和他人的都同時會觸發,可以取得 connectionId
session.on('connectionDestroyed', handleConnectionDestroyed); // 只有他人的會觸發,可以取得 connectionId
session.on('streamCreated', handleStreamCreated); // 只有他人的會觸發,可以取得 stream 和 connectionId
session.on('streamDestroyed', handleStreamDestroyed); // 只有他人的會觸發,可以取得 stream 和 connectionId
// session.on('sessionDisconnected', handleSessionDisconnected); // 只有自己的會觸發,可以取得 connectionId
signal
session.on('signal', (event) => {
event.from.id; // 發送 signal 的 id
event.data; // signal 中一併傳來的 data
});
Signal
Signal 物件
{
"type": "signal:foo",
"cancelable": false,
"_defaultPrevented": false,
"data": "bar",
"from": {
/* Connection Object */
},
"target": {
/* Session Object */
}
}
發送 Signal
// signal(signal, completionHandler)
session.signal({
type: "foo",
to: recipientConnection; // a Connection object
data: "hello"
},
function(error) {
if (error) {
console.log("signal error: " + error.message);
} else {
console.log("signal sent");
}
}
);
Session 物件
// Session 物件
{
"id": "1_MX40NjQ2MTY0Mn5-MTU3NzY3...",
"sessionId": "1_MX40NjQ2MTY0Mn5-MTU3NzY3...",
"currentState": "connected",
"connection": {}, // Connection Object
"connections": {},
"streams": {},
"archives": {},
"capabilities": {
"publish": 1,
"subscribe": 1,
"forceUnpublish": 0,
"forceDisconnect": 0,
"supportsWebRTC": 1
},
"token": "T1==cGFydG5lcl9p...",
"previousState": "connecting",
"apiKey": "46461642",
"staticConfig": {},
"sessionInfo": {}
}
Connect
ConnectionEvent 物件
- Connection Event:
connectionCreated
,connectionDestroyed
ConnectionEvent 比較是從整個 session 的角度觀看,在 session 中:
- 當有新的 client(包含自己)進入 session,或者
- 自己第一次進入 session 時,原本就在 session 內的 client
都會觸發 connectionCreated
事件。當有 client 從 session 中離開時(不包含自己)會觸發 connectionDestroyed
事件。
ConnectionEvent 中會包含有 Connection Object
, cancelable
和 reason
等屬性:
// ConnectionEvent Object
{
"type": "connectionCreated",
"cancelable": false,
"connection": { /*...*/ }, // Connection Object
"reason": undefined,
"target": { /*...*/ }, // Session Object
}
Connection 物件
Connection 物件
每一個連進 session 的使用者都會有一個獨特的 connection ID。從這裡可以取得 Connection
物件:
session.connection
:在 Session 物件中有一個 Connection 屬性,可以在session.connect()
的 callback 中取得connectionId
session.connect(TOKEN_PUBLISHER, (error) => {
if (error) {
handleError(error);
} else {
// 這裡的 session 可以取得 connection 物件
console.log(session.connection);
}
});
connectCreated
事件中,在event
內包含有connect
物件:
session.on('connectionCreated', (event) => {
// 這裡的 event 可以取得 connection 物件
console.log(event.connection);
});
Connection 物件中包含:
connectionId
creationTime
:時間戳記(毫秒),可直接帶入new Date()
使用data
:在後端產生 Token 時可以定義此資料
// Connection Object
{
"id": "6bd8a316-00bf-461f-a0cd-053cab7b20b4",
"connectionId": "6bd8a316-00bf-461f-a0cd-053cab7b20b4",
"creationTime": 1580443544160,
"data": "'{\"name\":\"Hamama\",\"deviceSystemVersion\":\"ios 13.3\"}'",
"capabilities": {
"supportsWebRTC": true
},
"permissions": {
"publish": 1,
"subscribe": 1,
"forceUnpublish": 0,
"forceDisconnect": 0,
"supportsWebRTC": 1
},
"quality": null
}