Skip to content

Instantly share code, notes, and snippets.

@zapo
Created October 20, 2018 22:33
Show Gist options
  • Select an option

  • Save zapo/063afe44bb43737b0fe45a3d3f96b97a to your computer and use it in GitHub Desktop.

Select an option

Save zapo/063afe44bb43737b0fe45a3d3f96b97a to your computer and use it in GitHub Desktop.

Revisions

  1. zapo created this gist Oct 20, 2018.
    52 changes: 52 additions & 0 deletions chat.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    const connection = new RTCPeerConnection({ iceServers: [{ urls: 'stun:stun.l.google.com:19302' }] });

    connection.onicecandidate = (e) => {
    if (e.candidate) {
    localStorage.setItem('candidate', JSON.stringify(e.candidate));
    }
    };

    connection.oniceconnectionstatechange = (e) => {
    console.log(connection.iceConnectionState);
    }

    const channel = connection.createDataChannel('chat', { negociated: true, id: 'very secret' });
    channel.onmessage = ({ data }) => console.log(data);

    window.sendMessage = (message) => {
    channel.send(message)
    }

    async function createOffer() {
    const offer = await connection.createOffer();
    connection.setLocalDescription(offer)
    localStorage.setItem('offer', JSON.stringify(offer));
    }

    async function createAnswer(offer) {
    connection.setRemoteDescription(
    new RTCSessionDescription(offer)
    );

    const answer = await connection.createAnswer();
    connection.setLocalDescription(answer)
    localStorage.setItem('answer', JSON.stringify(answer))
    }

    async function acceptAnswer(answer) {
    connection.setRemoteDescription(new RTCSessionDescription(answer));
    };

    window.addEventListener('storage', ({ key, newValue }) => {
    switch(key) {
    case 'offer':
    createAnswer(JSON.parse(newValue));
    break;
    case 'answer':
    acceptAnswer(JSON.parse(newValue));
    break;
    case 'candidate':
    connection.addIceCandidate(JSON.parse(newValue));
    break;
    }
    });