Last active
August 29, 2015 14:00
-
-
Save spq24/11382339 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ChatsController < ApplicationController | |
def index | |
@chats = Chat.order("created_at DESC") | |
end | |
def new | |
@chat = Chat.new | |
end | |
def create | |
session_properties = {OpenTok::SessionPropertyConstants::P2P_PREFERENCE => "enabled"} | |
session = OTSDK.create_session nil, session_properties | |
session_id = session.session_id | |
@chat = Chat.new(chat_params) | |
if @chat.save | |
redirect_to @chat | |
else | |
format.html { render :controller => 'chats', :action => "index" } | |
end | |
end | |
def party | |
@chat = Chat.find(params[:id]) | |
@session_id = @chat.sessionId | |
end | |
def show | |
@chat = Chat.find(params[:id]) | |
end | |
private | |
def chat_params | |
params.require(:chat).permit(:sessionId,:user_id,:token) | |
end | |
end | |
initializers/opentok.rb | |
# Creating an OpenTok Object | |
API_KEY = ENV['opentok_api_key'] # replace with your OpenTok API key | |
API_SECRET = ENV['opentok_secret_key'] # replace with your OpenTok API secret | |
OTSDK = OpenTok::OpenTokSDK.new API_KEY, API_SECRET | |
new.html.erb | |
<%= form_for(@chat) do |f| %> | |
<%= f.hidden_field sessionId: sessionId %> | |
<%= f.hidden_field token: token %> | |
<%= f.submit %> | |
<% end %> | |
show.html.erb | |
<div id="myPublisherDiv"></div> | |
<script type="text/javascript"> | |
// Initialize API key, session, and token... | |
// Think of a session as a room, and a token as the key to get in to the room | |
// Sessions and tokens are generated on your server and passed down to the client | |
var apiKey = "44745342"; | |
var sessionId = <%= @chat.sessionId %>; | |
var token = <%= @chat.token %>; | |
// Initialize session, set up event listeners, and connect | |
var session = TB.initSession(sessionId); | |
session.addEventListener('sessionConnected', sessionConnectedHandler); | |
session.connect(apiKey, token); | |
function sessionConnectedHandler(event) { | |
var publisher = TB.initPublisher(apiKey, 'myPublisherDiv'); | |
session.publish(publisher); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment