最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - OpenTok: What's the difference between the session and the token? - Stack Overflow

programmeradmin2浏览0评论

I'm trying to make a 2 persons video chat using OpenTok API, but I don't have a clue on how to generate a sessionId or a token and what's the difference between them.

I've looked into the provided examples, but they don't show how to generate them. So I would appreciate if someone could provide an example with explanations.

I'm trying to make a 2 persons video chat using OpenTok API, but I don't have a clue on how to generate a sessionId or a token and what's the difference between them.

I've looked into the provided examples, but they don't show how to generate them. So I would appreciate if someone could provide an example with explanations.

Share Improve this question edited Jul 21, 2016 at 20:32 Lucas Huang 4,0163 gold badges21 silver badges29 bronze badges asked Mar 20, 2011 at 9:41 CodeOverloadCodeOverload 48.6k56 gold badges133 silver badges223 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

SessionIDs identify the video chat that you want to connect to. Many people can connect and publish video streams to the same session. You see and hear other people in a session based on which video streams your browser subscribes to in that session. Because your application controls who can publish and who subscribes to whom, you can create a wide variety of video chat topologies in your session (eg. 1:1, video conference, talk show, security cam, etc.).

Tokens are a security/authentication mechanism. When you initiate a connection to a given session, you must provide a token that was generated using the same credentials that created the session. Tokens prevent other sites from "party crashing" a session that you have created, if they manage to get their hands on your sessionId.

Furthermore, when you generate a token, you can imbue it with a role, which tells the OpenTok infrastructure what types of actions to allow. For instance, you can decide to give a particular connection moderation rights by initiating that connection with a token that has the moderator role.

Here is a very basic example of how to generate the session ID and token:

<?php
    require_once 'SDK/API_Config.php';
    require_once 'SDK/OpenTokSDK.php';

    $apiObj = new OpenTokSDK(API_Config::API_KEY, API_Config::API_SECRET);

    $session = $apiObj->create_session($_SERVER["REMOTE_ADDR"]);

    $sessionId = $session->getSessionId();
    $token = $apiObj->generate_token($sessionId, "moderator");
?>

You can then drop these values into the JS code like this:

<script type="text/javascript">
    var apiKey = <?php echo API_Config::API_KEY; ?> ;
    var sessionId = "<?php echo $sessionId; ?>";
    var token = "<?php echo $token; ?>";

    var session;
    var publisher;
    var subscribers = {};

    session = TB.initSession(sessionId);

    //Video chat event listeners
    session.addEventListener('sessionConnected', sessionConnectedHandler);
    session.addEventListener('streamCreated', streamCreatedHandler);

    session.connect(apiKey, token);
</script>
发布评论

评论列表(0)

  1. 暂无评论