Skip to content

Commit 59b6d43

Browse files
committed
Add xterm.js web support
1 parent af75f16 commit 59b6d43

5 files changed

Lines changed: 245 additions & 3 deletions

File tree

‎attach.js‎

Lines changed: 93 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎client/.gitignore‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎index.html‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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>

‎main.go‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"bytes"
66
"compress/zlib"
77
"encoding/base64"
8+
"encoding/json"
89
"flag"
910
"fmt"
1011
"io/ioutil"
@@ -123,7 +124,7 @@ func runHost() (err error) {
123124
case *datachannel.PayloadString:
124125
fmt.Printf("Message '%s' from DataChannel '%s' payload '%s'\n", p.PayloadType().String(), d.Label, string(p.Data))
125126
data := string(p.Data)
126-
if data[:4] == "size" {
127+
if len(data) > 4 && data[:4] == "size" {
127128
coords := strings.Split(data[5:], ",")
128129
out := [4]int{}
129130
fmt.Println(coords)
@@ -142,8 +143,27 @@ func runHost() (err error) {
142143
if err != nil {
143144
glog.Error(err)
144145
}
145-
146146
}
147+
if len(data) > 2 && data[:2] == `["` {
148+
var msg []string
149+
json.Unmarshal(p.Data, &msg)
150+
if msg[0] == "stdin" {
151+
_, err := ptmx.Write([]byte(msg[1]))
152+
if err != nil {
153+
log.Fatal(err)
154+
}
155+
}
156+
if msg[0] == "set_size" {
157+
// TODO: error checking, everywhere
158+
var size []int
159+
json.Unmarshal(p.Data, &size)
160+
ws, _ := pty.GetsizeFull(ptmx)
161+
ws.Rows = uint16(size[1])
162+
ws.Cols = uint16(size[2])
163+
pty.Setsize(ptmx, ws)
164+
}
165+
}
166+
147167
// _, err := stdinWriter.Write(p.Data)
148168
// log.Println(err)
149169
case *datachannel.PayloadBinary:

‎readme.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# WebRTTY
2+
3+
```bash
4+
go get github.com/maxmcd/webrtty
5+
```
6+
7+

0 commit comments

Comments
 (0)
⌂ 换网址