Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mb nostr #60

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,84 @@ internship for Summer of 2024.**
You can [email me](mailto:[email protected]) if you so desire. I'm
happy to talk about education, programming languages, cycling, and more.

## Latest nostr

<div id="nostr-container"><ul></ul></div>

<script type="text/javascript">
const relay = "wss://offchain.pub";
const socket = new WebSocket(relay);
const pubkey =
"ff6560d3d0c180d8be922541650ca53debdfa50d10d30d05e1320c0a04b64584";
const messages = [];

function subscribe(pubkey) {
const filter = {
authors: [pubkey],
};
const subscription = ["REQ", "my-sub", filter];
socket.send(JSON.stringify(subscription));
}

function escapeHTML(unsafeText) {
// https://stackoverflow.com/a/48054293/569183
const div = document.createElement("div");
div.innerText = unsafeText;
return div.innerHTML;
}

function renderMessages(messages) {
const list = document.createElement("ul");
list.setAttribute("style", "list-style-type: none; padding-left: 0px; padding-bottom: 10px;");
for (const message of messages) {
const text = message.content;
const date = new Date(message.created_at * 1000);
const date_str = date.toLocaleDateString("en-US");
const time_str = date.toLocaleTimeString("en-US");
const element = document.createElement("li");
element.innerText = `${text}\n ${time_str} on ${date_str}`;
element.setAttribute("style", "padding-bottom: 10px;");
list.appendChild(element);
}
const old_list = document.querySelector("#nostr-container ul");
document.querySelector("#nostr-container").replaceChild(list, old_list);
}

socket.addEventListener("open", function (event) {
subscribe(pubkey);
});

socket.addEventListener("message", function (event) {
const message_data_json = event.data;
const message_data = JSON.parse(message_data_json);
if (!(message_data instanceof Array)) {
console.log("Invalid message: expected Array", message_data);
return;
}
if (message_data.length < 2) {
console.log("Invalid message: too short", message_data);
return;
}
const message_type = message_data[0];
if (message_type !== "EVENT") {
// Can ignore EOSE... for now? TODO: do we need to re-open a sub?
return;
}
if (message_data.length !== 3) {
console.log("Invalid EVENT message: too short", message_data);
return;
}
const event_data = message_data[2];
if (event_data.kind !== 1) {
// Ignore non text-note kinds for now
return;
}
messages.push(event_data);
messages.sort((a, b) => b.created_at - a.created_at);
renderMessages(messages.slice(0, 5));
});
</script>

## I like making things.

That's probably an understatement. Here are some of my favorite projects:
Expand Down
67 changes: 67 additions & 0 deletions webrtc3.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>WebRTC chat</title>
</head>
<body>
<h1>Open the JS console!</h1>
<script>
var RTCPeerConnection = window.RTCPeerConnection || webkitRTCPeerConnection || mozRTCPeerConnection;
var peerConn = new RTCPeerConnection({'iceServers': [
// {'urls': [ 'stun:stun.l.google.com:19302', ]},
{ urls: 'stun:freeturn.net:5349' },
{ urls: 'turn:freeturn.net:3478', username: 'free', credential: 'free' },
// { urls: 'turns:freeturn.tel:5349', username: 'free', credential: 'free' },
]});
console.log('Call create(), or join("some offer")');
function create() {
console.log("Creating ...");
var dataChannel = peerConn.createDataChannel('test');
dataChannel.onopen = (e) => {
window.say = (msg) => { dataChannel.send(msg); };
console.log('Say things with say("hi")');
};
dataChannel.onmessage = (e) => { console.log('Got message:', e.data); };
peerConn.createOffer({})
.then((desc) => peerConn.setLocalDescription(desc))
.then(() => {})
.catch((err) => console.error(err));
peerConn.onicecandidate = (e) => {
if (e.candidate == null) {
console.log("Get joiners to call: ", "join(", JSON.stringify(peerConn.localDescription), ")");
}
};
window.gotAnswer = (answer) => {
console.log("Initializing ...");
peerConn.setRemoteDescription(new RTCSessionDescription(answer));
};
}

function join(offer) {
console.log("Joining ...");

peerConn.ondatachannel = (e) => {
var dataChannel = e.channel;
dataChannel.onopen = (e) => {
window.say = (msg) => { dataChannel.send(msg); };
console.log('Say things with say("hi")');
};
dataChannel.onmessage = (e) => { console.log('Got message:', e.data); }
};

peerConn.onicecandidate = (e) => {
if (e.candidate == null) {
console.log("Get the creator to call: gotAnswer(", JSON.stringify(peerConn.localDescription), ")");
}
};

var offerDesc = new RTCSessionDescription(offer);
peerConn.setRemoteDescription(offerDesc);
peerConn.createAnswer({})
.then((answerDesc) => peerConn.setLocalDescription(answerDesc))
.catch((err) => console.warn("Couldn't create answer"));
}
</script>
</body>
</html>
Loading