|
| 1 | + |
| 2 | +<!DOCTYPE html> |
| 3 | +<html> |
| 4 | +<head> |
| 5 | + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/xterm@3.8.0/dist/xterm.css"> |
| 6 | + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/xterm@3.8.0/dist/addons/fullscreen/fullscreen.css"> |
| 7 | + <script src="https://cdn.jsdelivr.net/npm/xterm@3.8.0/dist/xterm.js"></script> |
| 8 | + <script src="attach.js"></script> |
| 9 | + <script src="https://cdn.jsdelivr.net/npm/xterm@3.8.0/dist/addons/fullscreen/fullscreen.js"></script> |
| 10 | + <script src="https://cdn.jsdelivr.net/npm/xterm@3.8.0/dist/addons/fit/fit.js"></script> |
| 11 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/pako/1.0.6/pako.min.js"></script> |
| 12 | + <style> |
| 13 | + #terminal { |
| 14 | + position: fixed; |
| 15 | + top: 0; |
| 16 | + bottom: 0; |
| 17 | + left: 0; |
| 18 | + right: 0; |
| 19 | + width: auto; |
| 20 | + height: auto; |
| 21 | + z-index: 255; |
| 22 | + } |
| 23 | + </style> |
| 24 | +</head> |
| 25 | + |
| 26 | +<body> |
| 27 | + <div id="terminal" /> |
| 28 | + <script> |
| 29 | + Terminal.applyAddon(attach); |
| 30 | + Terminal.applyAddon(fullscreen); |
| 31 | + Terminal.applyAddon(fit); |
| 32 | + |
| 33 | + |
| 34 | + var term = new Terminal(); |
| 35 | + term.open(document.getElementById("terminal")); |
| 36 | + term.toggleFullScreen(); |
| 37 | + term.fit(); |
| 38 | + window.onresize = () => { |
| 39 | + term.fit(); |
| 40 | + } |
| 41 | + term.write("Welcome to the WebRTTY web client.\n\r"); |
| 42 | + term.write("Run webrtty and paste the offer message below:\n\r"); |
| 43 | + let firstInput = false; |
| 44 | + term.on("data", data => { |
| 45 | + if (!firstInput){ |
| 46 | + term.reset() |
| 47 | + try { |
| 48 | + startSession(data) |
| 49 | + } |
| 50 | + catch(err) { |
| 51 | + term.write(`There was an error with the offer: ${data}\n\r`) |
| 52 | + term.write("Try entering the message again: ") |
| 53 | + return |
| 54 | + } |
| 55 | + firstInput = true; |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + let pc = new RTCPeerConnection({ |
| 60 | + iceServers: [ |
| 61 | + { |
| 62 | + urls: "stun:stun.l.google.com:19302" |
| 63 | + } |
| 64 | + ] |
| 65 | + }); |
| 66 | + |
| 67 | + let log = msg => { |
| 68 | + term.write(msg + "\n\r") |
| 69 | + }; |
| 70 | + |
| 71 | + let sendChannel = pc.createDataChannel("data"); |
| 72 | + sendChannel.onclose = () => console.log("sendChannel has closed"); |
| 73 | + sendChannel.onopen = () => { |
| 74 | + term.reset() |
| 75 | + term.terminadoAttach(sendChannel); |
| 76 | + term.__getMessage = (ev) => { |
| 77 | + term.write(ev) |
| 78 | + } |
| 79 | + console.log("sendChannel has opened"); |
| 80 | + }; |
| 81 | + // sendChannel.onmessage = e => {} |
| 82 | + |
| 83 | + pc.onsignalingstatechange = e => log(pc.signalingState) |
| 84 | + pc.oniceconnectionstatechange = e => log(pc.iceConnectionState) |
| 85 | + pc.onicecandidate = event => { |
| 86 | + if (event.candidate === null) { |
| 87 | + term.write("Answer created. Send the following answer to the host:\n\r\n\r") |
| 88 | + term.write(btoa(pako.deflate(pc.localDescription.sdp, { to: "string" }))) |
| 89 | + |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + pc.onnegotiationneeded = e => console.log(e) |
| 94 | + |
| 95 | + window.sendMessage = () => { |
| 96 | + let message = document.getElementById("message").value; |
| 97 | + if (message === "") { |
| 98 | + return alert("Message must not be empty"); |
| 99 | + } |
| 100 | + |
| 101 | + sendChannel.send(message); |
| 102 | + }; |
| 103 | + |
| 104 | + startSession = (data) => { |
| 105 | + pc.setRemoteDescription( |
| 106 | + new RTCSessionDescription({ |
| 107 | + type: "offer", |
| 108 | + sdp: pako.inflate(atob(data), { to: "string" }) |
| 109 | + }) |
| 110 | + ).catch(log) |
| 111 | + pc.createAnswer().then(d => pc.setLocalDescription(d)).catch(log) |
| 112 | + }; |
| 113 | + </script> |
| 114 | + Browser base64 Session Description <textarea id="localSessionDescription" readonly="true"></textarea> <br /> |
| 115 | + |
| 116 | + <br /> |
| 117 | + |
| 118 | + Message: <textarea id="message">This is my DataChannel message!</textarea> <br/> |
| 119 | + <button onclick="window.sendMessage()"> Send Message </button> <br /> |
| 120 | + |
| 121 | + <div id="logs"></div> |
| 122 | +</body> |
| 123 | +</html> |
0 commit comments