Skip to content

Instantly share code, notes, and snippets.

@GleasonK
Last active February 4, 2016 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GleasonK/5ac4cbcf2ec80a9fd629 to your computer and use it in GitHub Desktop.
Save GleasonK/5ac4cbcf2ec80a9fd629 to your computer and use it in GitHub Desktop.
WebRTC-TicTacToe code snippets
<div id="tic-tac-box" style="float: left; width: 47%;"></div>
<div style="float: left; width: 50%;">
<div id="video-chat" hidden="true">
<div id="vid-box"></div>
<button onclick="end()">End Call</button>
</div>
<form name="loginForm" id="login" action="#" onsubmit="return login(this);">
<input type="text" name="username" id="username" placeholder="Enter A Username"/>
<input type="submit" name="login_submit" value="Log In">
</form>
<form name="callForm" id="call" action="#" onsubmit="return makeCall(this);">
<input type="text" name="number" id="call" placeholder="Enter User To Call!"/>
<input type="submit" value="Call">
</form>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdn.pubnub.com/pubnub-3.7.23.min.js"></script>
<script src="http://kevingleason.me/WebRTC-TicTacToe/js/webrtc-2.0.0.js"></script>
<script src="http://kevingleason.me/WebRTC-TicTacToe/js/tictactoe.js"></script>
var video_hold = document.getElementById("video-chat");
var video_out = document.getElementById("vid-box");
function login(form) {
user_name = form.username.value || "Anonymous";
var phone = window.phone = PHONE({
number : user_name, // listen on username line else Anonymous
publish_key : 'your_pub_key',
subscribe_key : 'your_sub_key',
datachannels : true, // Enable Data Channels
});
phone.ready(function(){ form.username.style.background="#55ff5b"; form.login_submit.hidden="true"; });
phone.receive(function(session){
session.connected(function(session) { video_hold.hidden=false; video_out.appendChild(session.video); });
session.ended(function(session) { video_out.innerHTML=''; });
});
... // Prepare Data Channel
return false;
}
function makeCall(form){
if (!window.phone) alert("Login First!");
else phone.dial(form.number.value);
return false;
}
function end(){
if (!window.phone) return;
window.phone.hangup();
video_hold.hidden = true;
}
var game_board = document.getElementById("tic-tac-box");
var user_name = "";
var game_board = ticTacToe(game_board);
game_board.onSquareClicked(
function(squareNum){ // Number of the box that was clicked
if (!window.phone) return;
var data = {square:squareNum, username:user_name};
window.phone.sendData(data);
}
)
function onDataReceived(msg){
var sqr_num = msg.square;
game_board.markBox(sqr_num);
}
function login(form) {
// Setup phone
// Ready and Receive Callbacks
...
phone.datachannel(onDataReceived);
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment