Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. import React from 'react';
  2. import { OTSession, OTPublisher, OTStreams, OTSubscriber, createSession } from 'opentok-react';
  3.  
  4.  
  5.  
  6. export class TokBoxChat extends React.Component {
  7. constructor(props){
  8. super(props);
  9. this.startSession = this.startSession.bind(this);
  10. this.joinSession = this.joinSession.bind(this);
  11. this.endSession = this.endSession.bind(this);
  12. this.state = {
  13. token: false,
  14. }
  15. startSession(){
  16.  
  17. const sucessful = (response) => this.setState({token: response}); // store the token in state
  18.  
  19. Meteor.call('video.createSession', '1', function(error, response){
  20. if (error) { Bert.alert(error.reason, 'danger'); return; } // throw error if we get an error
  21. Bert.alert('session created', 'success'); // fire alert
  22. sucessful(response);// fire method that calls this.setState
  23. });
  24.  
  25. }
  26. joinSession(){
  27. const sucessful = (response) => this.setState({token: response}); // store the token in state
  28.  
  29. Meteor.call('video.joinSession', '1', function(error, response){
  30. if (error) { Bert.alert(error.reason, 'danger'); return; } // throw error if we get an error
  31. Bert.alert('session joined', 'success'); // fire alert
  32. sucessful(response);// fire method that calls this.setState
  33. });
  34.  
  35. }
  36. endSession(){
  37. const sucessful = () => this.setState({token: false}); // store the token in state
  38.  
  39. Meteor.call('video.endSession', '1', function(error, response){
  40. if (error) { Bert.alert(error.reason, 'danger'); return; } // throw error if we get an error
  41. Bert.alert('session ended', 'success'); // fire alert
  42. sucessful();// fire method that calls this.setState
  43. });
  44. }
  45. render() {
  46. const { video } = this.props; //bring our variable in and destructure it from this.props for brevity
  47. return (
  48. <div>
  49. {!video.sessionOn && <button onClick={this.startSession}>start session</button>}
  50. {video.sessionOn && this.state.token && <button onClick={this.endSession}>end session</button>}
  51. {video.sessionOn && !this.state.token && <button onClick={this.joinSession}>join session</button>}
  52. {this.state.token && video.currentSessionId && video.sessionOn && <OTSession
  53. apiKey={Meteor.settings.public.tokbox.apiKey}
  54. sessionId={video.currentSessionId}
  55. token={this.state.token}>
  56. <OTPublisher />
  57. <OTStreams>
  58. <OTSubscriber />
  59. </OTStreams>
  60. </OTSession>}
  61.  
  62. </div>
  63. );
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement