Overview
The front-end MediaClient
module implements the client of the WebMedia
data channel. Refer to this module according to the following code:
<script src="mediaclient.min.js"></script>
Support
The following shows MediaClient
module APIs.
API |
---|
MediaClient |
client.open |
client.close |
client.send |
client.emit |
MediaClient Class
new MediaClient(origin, shakeHandle[, opts])
-
origin
{String} Web media data channel url origin(protocol + host). Currently supports thewebsocket
protocol. -
shakeHandle
{Function} AsshakeHandle
event callback. The user should establish a stream channel in this callback. Arguments:-
client
{MediaClient}MediaClient
self object. -
path
{String} Recommended stream channel url path.
-
-
opts
{Object} Options:-
timeout
{Integer} When the client opened timeout, the client will be closed and theerror
event will be triggered, default: 30000s. -
path
{String} Data channel url path, default: '/live'. -
token
{String} EdgerOS account token, which can be obtained by EdgerOS Web-SDK. -
srand
{String} EdgerOS login serial number, which can be obtained by EdgerOS Web-SDK.
-
- Returns: {MediaClient}
MediaClient
client object.
Create an WebMedia
client in the Front-End.
Example
var client = new MediaClient('ws://xxxx.com', (client, path) => {
// Media stream connect.
});
MediaClient Object
client.open(opts)
-
opts
{Object} Option:-
token
{String} EdgerOS account token, which can be obtained by EdgerOS Web-SDK. -
srand
{String} EdgerOS login serial number, which can be obtained by EdgerOS Web-SDK.
-
Connect to server and create data channel.
client.close()
Disconnect and close the channel.
client.send(opts, data[, cb])
-
opts
{Object | Undefined} Event options. -
data
{String | Array | Object} Event data. -
cb
{Function} Remote callback. Argument:-
client
{MediaClient} Media client self. -
opts
{Object | Undefined} Options reply by remote client. -
data
{String | Array | Object} Data reply by remote client.
-
Send message
event to client by data channel when client is opened.If the cb
is valid and the remote client replies to the event, cb
will be called.
Event protocol see WebMedia Data Channel Protocol
.
Example
- Send message event:
client.send({type: 'mediaInfo'}, {width: 480, height: 320});
- Send message event with reply:
client.send({type: 'mediaInfo'}, {width: 480, height: 320}, (client, opts, data) => {
// Do something.
});
- Receive message event:
client.on('message', (client, opts, data, cb) => {
// Do something.
if (cb) {
cb(opts2, data2); // cb(opts, data)
}
});
emit(event, ...args[, cb])
-
event
{String} Event name. -
args
{String | Array | Object} Event args. -
cb
{Function} Remote callback. Argument:-
client
{MediaClient} Media client self. -
args
{Any} Arguments reply by remote client.
-
Send event
event to client by data channel when client is opened.If the cb
is valid and the remote client replies to the event, cb
will be called.
Users cannot use reserved events. Hold event:
-
'error'
,'open'
,'close'
,'pause'
,'resume'
,'message'
,'data'
,'shakeHandle'
.
Event protocol see Data Channel Protocol
.
Example
- Send event:
client.emit('addRoles', {id: 1, name: 'xming', age: '22'}, {id: 2, name: 'xli', age: '25'});
- Send event with reply:
client.emit('addRoles', {id: 1, name: 'xming', age: '22'}, {id: 2, name: 'xli', age: '25'}, (client, result) => {
if (result) {
console.log('Add roles success.');
}
});
- Receive event:
client.on('addRoles', (client) => {
var args = Array.prototype.slice.call(arguments);
for (var i = 1; i < args.length; i++) {
var role = args[i];
// Add role.
}
});
- Receive event with reply:
client.on('addRole', (client, role, cb) => {
// Add role.
cb('ok'); // cb(...args)
});
MediaClient Events
The first parameter of all events is MediaClient
object itself.
shakeHandle
-
client
{MediaClient}MediaClient
object self. -
path
{String} Recommended stream channel url path.
Emit shakeHandle
event when client ready.
open
-
client
{MediaClient}MediaClient
object self.
Emit open
event when client open (after sttream channel connected).
close
-
client
{MediaClient}MediaClient
object self.
Emit close
event when client closed.
error
-
client
{MediaClient}MediaClient
object self.
Emit error
event when client error. The client will close automatic.
data
-
client
{MediaClient}MediaClient
object self. -
opts
{Object | Undefined} Options send by remote client. -
data
{String | Array | Object} Data send by remote client.
Emit data
event when receive data
event from remote.
message
-
client
{MediaClient}MediaClient
object self. -
opts
{Object | Undefined} Options send by remote client. -
data
{String | Array | Object} Data send by remote client.
Emit message
event when receive message
event from remote.