Advertisement
Guest User

Untitled

a guest
Dec 7th, 2023
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 9.36 KB | Source Code | 0 0
  1.  
  2.   let sendChannel;
  3.   let receiveChannel;
  4.   let localConnection;
  5.   let remoteConnection;
  6.   let is_girl = false;
  7.   let is_boy = false;
  8.   let girlCodeExecuted = false;
  9.   let boyCodeExecuted = false;
  10.   let connected = false;
  11.   let hasReceivedAnswer = false;
  12.   let hasReceivedoffer = false;
  13.   let intervalID;
  14.   let offer;
  15.   let answer;
  16.  
  17.   const configuration = {
  18.   iceServers: [
  19.     {
  20.       urls: "stun:stun.l.google.com:19302",
  21.     },
  22.     {
  23.       urls: "turn:turn.anyfirewall.com:443?transport=tcp",
  24.       username: "webrtc",
  25.       credential: "webrtc",
  26.     },
  27.   ],
  28. };
  29.  
  30.  
  31.  
  32.   function skip() {
  33.       if (is_boy) {
  34.           localConnection.close();
  35.  
  36.       } else if (is_girl) {
  37.           remoteConnection.close();
  38.  
  39.       }
  40.      
  41.   }
  42.  
  43.   function onConnect() {
  44.       var skipButton = document.getElementById('skip');
  45.       skipButton.removeAttribute('disabled');
  46.       connected = true;
  47.       clearInterval(intervalID)
  48.       ulll.innerHTML = '';
  49.   }
  50.  
  51.   function onDisconnect() {
  52.       sendChannel = undefined;
  53.       receiveChannel = undefined;
  54.       localConnection = undefined;
  55.       remoteConnection = undefined;
  56.       offer = undefined;
  57.       answer = undefined;
  58.       is_girl = false;
  59.       is_boy = false;
  60.       girlCodeExecuted = false;
  61.       boyCodeExecuted = false;
  62.       hasReceivedAnswer = false;
  63.       hasReceivedoffer = false;
  64.       connected = false;
  65.       console.log('ondisconnect');
  66.       var skipButton = document.getElementById('skip');
  67.       skipButton.setAttribute('disabled', '');
  68.       intervalID = setInterval(sendhello, 1000);  
  69.       reconnect();
  70.    
  71.   }
  72.  
  73.   var socket = new WebSocket('wss://' + window.location.host + '/text/');
  74.   var ulll = document.querySelector("#ulll");
  75.  
  76.   function reconnect() {
  77.         console.log("Reconnecting...");
  78.       socket = new WebSocket('wss://' + window.location.host + '/text/');
  79.       socket.onmessage = async function (event) {
  80.       var data = JSON.parse(event.data);
  81.       console.log(data);
  82.       if (data.waiting_users) {
  83.           if (data.waiting_users[0] === '{{user.username}}') {
  84.               await boy();
  85.               is_boy = true;
  86.               console.log('hi boy');
  87.           }
  88.  
  89.           if (data.waiting_users[1] === '{{user.username}}') {
  90.               is_girl = true;
  91.               console.log('hi girl');
  92.           }
  93.       }
  94.       offer = await data.offer;
  95.  
  96.       if (offer && is_girl && !hasReceivedoffer) {
  97.           console.log('entered stage2');
  98.           await girl(offer);
  99.           hasReceivedoffer = true
  100.       }
  101.  
  102.       answer = await data.answer;
  103.  
  104.       if(answer){clearInterval(intervalID)}
  105.      
  106.       if (answer&& is_boy && !hasReceivedAnswer) {
  107.         await localConnection.setRemoteDescription(answer);
  108.         console.log('boy just received answer');
  109.         hasReceivedAnswer = true;
  110.    
  111.       }
  112.   };
  113.   }
  114.  
  115.   async function boy() {
  116.     if (!boyCodeExecuted) {
  117.         console.log('boy is running');
  118.       localConnection = new RTCPeerConnection(configuration);
  119.  
  120.       localConnection.onicecandidate = e => {
  121.           console.log("ICE candidate event:", e);
  122.           if (localConnection.iceGatheringState === 'complete') {
  123.               console.log("NEW ice candidate!!");
  124.               socket.send(JSON.stringify({ offer: localConnection.localDescription }));
  125.           }
  126.       };
  127.  
  128.       sendChannel = localConnection.createDataChannel("sendChannel");
  129.       sendChannel.onmessage = e => {
  130.           console.log("Received message on sendChannel:", e.data);
  131.           let li = document.createElement('li');
  132.           li.id = 'you';
  133.           li.textContent = e.data;
  134.           li.classList.add("new-message");
  135.           ulll.appendChild(li);
  136.       }
  137.  
  138.       sendChannel.onopen = e => {
  139.           console.log("sendChannel opened");
  140.           sendChannel.send('به {{ user.name }} {{user.age}} ساله از {{ user.city }} متصل شدید باهم دوست باشید :)');
  141.           socket.send(JSON.stringify({ d: 'hola' }));
  142.           socket.close();
  143.           console.log('boy is connected');
  144.           onConnect();
  145.       }
  146.  
  147.       sendChannel.onclose = e => {
  148.           console.log("sendChannel closed");
  149.           ulll.innerHTML = '<li>درحال پیدا کردن کاربر ...</li>';
  150.           console.log("closed!");
  151.          
  152.           console.log('boy is disconnected');
  153.           onDisconnect();
  154.       };
  155.  
  156.       let offer = await localConnection.createOffer();
  157.       console.log("Created offer:", offer);
  158.       await localConnection.setLocalDescription(offer);
  159.  
  160.       localConnection.onclose = event => {
  161.           console.log('Connection closed in boy function');
  162.  
  163.       };
  164.       boyCodeExecuted = true
  165.     }
  166.   }
  167.  
  168.   async function girl(offer) {
  169.     if (!girlCodeExecuted) {
  170.         console.log('girl is running');
  171.         remoteConnection = new RTCPeerConnection(configuration);
  172.  
  173.         remoteConnection.ondatachannel = e => {
  174.             console.log("Data channel event:", e);
  175.             receiveChannel = e.channel;
  176.             receiveChannel.onmessage = e => {
  177.                 console.log("Received message on receiveChannel:", e.data);
  178.                 let li = document.createElement('li');
  179.                 li.id = 'you';
  180.                 li.textContent = e.data;
  181.                 li.classList.add("new-message");
  182.                 ulll.appendChild(li);
  183.             };
  184.             receiveChannel.onopen = e => {
  185.                 console.log("receiveChannel opened");
  186.                 receiveChannel.send('به {{ user.name }} {{user.age}} ساله از {{ user.city }} متصل شدید باهم دوست باشید :)');
  187.                 socket.close();
  188.                 console.log('girl is connected');
  189.                 onConnect();
  190.             };
  191.  
  192.             receiveChannel.onclose = e => {
  193.                 console.log("receiveChannel closed");
  194.                 ulll.innerHTML = '<li>درحال پیدا کردن کاربر ...</li>';
  195.                 console.log("closed!!!!!!");
  196.                
  197.                 console.log('girl is disconnected');
  198.                 onDisconnect();
  199.             };
  200.             remoteConnection.channel = receiveChannel;
  201.         };
  202.  
  203.         if (remoteConnection.signalingState !== 'have-remote-offer') {
  204.             await remoteConnection.setRemoteDescription(new RTCSessionDescription(offer));
  205.  
  206.             let answer = await remoteConnection.createAnswer();
  207.             await remoteConnection.setLocalDescription(answer);
  208.  
  209.             remoteConnection.onclose = event => {
  210.                 console.log('Connection closed in girl function');
  211.             };
  212.  
  213.             console.log("Sending answer:", remoteConnection.localDescription);
  214.             socket.send(JSON.stringify({ answer: remoteConnection.localDescription }));
  215.            
  216.         }
  217.         girlCodeExecuted = true;
  218.     }
  219. }
  220.  
  221.   function girl_message(message) {
  222.       let li = document.createElement("li");
  223.       li.id = "me";
  224.       li.textContent = message;
  225.  
  226.       let ul = document.querySelector("#ulll");
  227.       ul.appendChild(li);
  228.  
  229.       li.classList.add("new-message");
  230.  
  231.       remoteConnection.channel.send(message);
  232.       document.getElementById('text').value = '';
  233.   }
  234.  
  235.   function boy_message(message) {
  236.       let li = document.createElement("li");
  237.       li.id = "me";
  238.       li.textContent = message;
  239.  
  240.       let ul = document.querySelector("#ulll");
  241.       ul.appendChild(li);
  242.  
  243.       li.classList.add("new-message");
  244.  
  245.       sendChannel.send(message);
  246.       document.getElementById('text').value = '';
  247.   }
  248.  
  249.   socket.onopen = () => { console.log('یه سوکت باز شد'); }
  250.  
  251.   function sendhello() {
  252.   if(!connected){
  253.     socket.send(JSON.stringify({helele : 'jehehe'}))
  254.     console.log('sending shit');}
  255. }
  256.  
  257.   intervalID = setInterval(sendhello, 1000);
  258.  
  259.   socket.onmessage = async function (event) {
  260.       var data = JSON.parse(event.data);
  261.       console.log(data);
  262.       if (data.waiting_users) {
  263.           if (data.waiting_users[0] === '{{user.username}}') {
  264.               await boy();
  265.               is_boy = true;
  266.               console.log('hi boy');
  267.           }
  268.  
  269.           if (data.waiting_users[1] === '{{user.username}}') {
  270.               is_girl = true;
  271.               console.log('hi girl');
  272.           }
  273.       }
  274.       offer = await data.offer;
  275.  
  276.       if (offer && is_girl && !hasReceivedoffer) {
  277.           console.log('entered stage2');
  278.           await girl(offer);
  279.           hasReceivedoffer = true
  280.       }
  281.  
  282.       answer = await data.answer;
  283.  
  284.       if(answer){clearInterval(intervalID)}
  285.      
  286.       if (answer&& is_boy && !hasReceivedAnswer) {
  287.         await localConnection.setRemoteDescription(answer);
  288.         console.log('boy just received answer');
  289.         hasReceivedAnswer = true;
  290.    
  291.       }
  292.   };
  293.  
  294.   socket.onclose = (event) => {
  295.       console.log('WebSocket connection closed with code ' + event.code);
  296.       clearInterval(intervalID)
  297.   };
  298.  
  299.   function send_message() {
  300.       let message = document.getElementById('text').value
  301.       console.log(message)
  302.       if (is_boy) {
  303.           boy_message(message);
  304.       } else if (is_girl) {
  305.           girl_message(message);
  306.           message = '';
  307.       }
  308.   }
  309.  
  310.   const textInput = document.getElementById('text');
  311.   textInput.addEventListener('keydown', (event) => {
  312.       if (event.key === 'Enter') {
  313.           send_message()
  314.       }
  315.   });
  316.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement