Last active
March 21, 2017 03:01
-
-
Save pandurang90/8a58452ff9ed3f3b3775 to your computer and use it in GitHub Desktop.
opentok.js
This file contains hidden or 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
<%= form_tag '#', class: 'chat_form' do %> | |
<%= text_field_tag ‘message’, id: ‘chat_msg’%> | |
<%= submit_tag ‘send’%> | |
<% end %> | |
<script src='//static.opentok.com/v2/js/opentok.min.js'></script> | |
<script type="text/javascript"> | |
$(document).ready(function(){ | |
var apiKey = <%= OPENTOK_API_KEY %> | |
// use same session_id and token that we have generated above. | |
var sessionID = "<%= @session_id %>" | |
var token = "<%= @token %>" | |
var session = OT.initSession(apiKey, sessionID); | |
session.connect(token, function(error) { | |
if (error) { | |
console.log('Unable to connect: ', error.message); | |
} | |
else {console.log('Connected to the session.');} | |
}); | |
// send message to text_chat channel using signal method on form submit | |
$(".chat_form").submit(function(event) { | |
event.preventDefault(); | |
if($('#chat_msg').val() != ""){ | |
session.signal({ | |
data:$('#chat_msg').val(), | |
type:"text_chat" | |
}, | |
function(error) { | |
$('#chat_msg').val(''); | |
} | |
); | |
} | |
}); | |
// listen for the signal on text_chat channel | |
// in order to receive message sent to channel text_chat | |
session.on('signal:text_chat', function(event) { | |
message = event.data | |
// do some stuff here with message, | |
// maybe show in chat window/store in database if needed | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment