Created
July 15, 2020 06:22
-
-
Save emad-elsaid/91fd7e303215bc6ccbd132f840315a8f to your computer and use it in GitHub Desktop.
WebRTC Chat server using simple-peer and Inbox server
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
<html> | |
<body> | |
<form id="userform"> | |
<label>Username</label> | |
<input id="username" type="text"> | |
<label>Password</label> | |
<input id="password" type="password"> | |
<label>Other peer Username (only if your the initiator)</label> | |
<input id="to" type="text"> | |
<button onclick="CreateUser()" type="button">Create</button> | |
</form> | |
<div id="log"></div> | |
<p> | |
<input type="text" id="message" /> | |
<button type="button" onclick="sendMessage()" >Send</button> | |
</p> | |
<script type="application/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/simple-peer/9.7.2/simplepeer.min.js"></script> | |
<script type="application/javascript" src="script.js" ></script> | |
</body> | |
</html> |
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
const server = "https://connect.inboxgo.org/inbox"; | |
async function SignalReceive(username, password) { | |
const headers = new Headers({ | |
Authorization: 'Basic ' + window.btoa(username + ":" + password) | |
}); | |
const response = await fetch(server, { | |
method: 'GET', | |
cache: 'no-cache', | |
headers: headers | |
}); | |
try { | |
var data = await response.json(); | |
const from = response.headers.get("X-From"); | |
console.log('Received', data); | |
return {from: from, data: data}; | |
} catch(e) { | |
return null; | |
} | |
} | |
async function SignalSend(username, password, to, message) { | |
console.log('Sending', message); | |
const headers = new Headers({ | |
Authorization: 'Basic ' + window.btoa(username + ":" + password) | |
}); | |
const response = await fetch(`${server}?to=${to}`, { | |
method: 'POST', | |
cache: 'no-cache', | |
headers: headers, | |
body: JSON.stringify(message) | |
}); | |
} | |
var peer; | |
async function CreateUser() { | |
const username = document.getElementById("username").value; | |
const password = document.getElementById("password").value; | |
let to = document.getElementById("to").value; | |
let poll = true; | |
document.getElementById("userform").style.display = "none"; | |
peer = new SimplePeer({ initiator: (to.length > 0) }) | |
peer.on('signal', data => { | |
if ( to.length > 0 ) SignalSend(username, password, to, data); | |
}) | |
peer.on('connect', () => { | |
poll = false; | |
peer.send('Hello ' + to); | |
}); | |
peer.on('data', data => { | |
var p = document.createElement("p"); | |
p.innerText = data; | |
document.getElementById("log").appendChild(p); | |
}); | |
peer.on('error', err => console.log('error', err)); | |
while ( poll ) { | |
try { | |
let message = await SignalReceive(username, password); | |
to = message.from; | |
peer.signal(message.data); | |
} catch (e) { | |
console.log("didn't receive a message or an error, retrying...", e); | |
} | |
} | |
} | |
function sendMessage() { | |
const message = document.getElementById("message").value; | |
peer.send(message); | |
} |
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
require 'webrick' | |
WEBrick::HTTPServer.new(:Port => ENV.fetch("PORT", 3000), :DocumentRoot => Dir.pwd).start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment