File size: 4,495 Bytes
2caf0dc
 
 
 
 
61cb741
2caf0dc
 
 
61cb741
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2caf0dc
 
 
61cb741
 
 
 
 
 
 
 
2caf0dc
 
 
 
 
 
 
c185c93
61cb741
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c185c93
2caf0dc
 
 
 
 
 
c185c93
61cb741
 
 
2caf0dc
c185c93
61cb741
 
 
 
 
 
2caf0dc
61cb741
2caf0dc
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Simple Chat</title>
    <script src="https://unpkg.com/peerjs@1.4.7/dist/peerjs.min.js"></script>
    <style>
        body {
            font-family: monospace;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
        }
        #message-container {
            height: 300px;
            border: 1px solid #ccc;
            padding: 10px;
            overflow-y: auto;
            margin-bottom: 10px;
        }
        #message-input {
            width: 80%;
            padding: 8px;
        }
        #send-btn {
            width: 18%;
            padding: 8px;
        }
    </style>
</head>
<body>
    <h1>Simple Chat</h1>
    <div id="message-container">
        <div>Connecting to chat server...</div>
    </div>
    <div>
        <input type="text" id="message-input" placeholder="Type message here...">
        <button id="send-btn">Send</button>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const messageContainer = document.getElementById('message-container');
            const messageInput = document.getElementById('message-input');
            const sendBtn = document.getElementById('send-btn');

            // Configuration for public STUN/TURN servers
            const peer = new Peer({
                config: {
                    'iceServers': [
                        { urls: 'stun:stun.l.google.com:19302' },
                        { urls: 'stun:global.stun.twilio.com:3478?transport=udp' },
                        { 
                            urls: 'turn:numb.viagenie.ca',
                            username: 'webrtc@live.com',
                            credential: 'muazkh'
                        }
                    ]
                }
            });

            let conn;
            const roomId = 'simple-chat-room'; // Fixed room ID for everyone to connect to

            peer.on('open', (id) => {
                messageContainer.innerHTML = `<div>Connected to chat room. Your ID: ${id}</div>`;
                
                // Connect to the fixed room
                conn = peer.connect(roomId);
                
                // Set up connection handlers
                conn.on('open', () => {
                    appendMessage('Connected to chat room');
                });

                conn.on('data', (data) => {
                    appendMessage(data);
                });

                conn.on('close', () => {
                    appendMessage('Connection closed');
                });
            });

            peer.on('connection', (connection) => {
                conn = connection;
                conn.on('data', (data) => {
                    appendMessage(data);
                });
            });

            function appendMessage(message) {
                const messageElement = document.createElement('div');
                messageElement.textContent = message;
                messageContainer.appendChild(messageElement);
                messageContainer.scrollTop = messageContainer.scrollHeight;
            }

            sendBtn.addEventListener('click', sendMessage);
            messageInput.addEventListener('keypress', (e) => {
                if (e.key === 'Enter') sendMessage();
            });

            function sendMessage() {
                const message = messageInput.value.trim();
                if (message && conn && conn.open) {
                    conn.send(message);
                    appendMessage(`You: ${message}`);
                    messageInput.value = '';
                }
            }
        });
    </script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=commit3r/temp" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>