Я ваще не андрестренд

mail@pastecode.io avatar
unknown
javascript
2 years ago
1.3 kB
7
Indexable
  <script>
        var roomName = {{ room_name_json }};
    
        var chatSocket = new WebSocket(
            'ws://' + window.location.host + '/ws/chat/' + roomName + '/');
    
        chatSocket.onmessage = function(e) {
            var data = JSON.parse(e.data);
            var message = data['message'];
            document.querySelector('#chat-log').value += (message + '\n');
            
        };
        
        chatSocket.onclose = function(e) {
            console.error('Chat socket closed unexpectedly');
        };
    
        document.querySelector('#chat-message-input').focus();
        document.querySelector('#chat-message-input').onkeyup = function(e) {
            if (e.keyCode === 13) {  // enter, return
                document.querySelector('#chat-message-submit').click();
            }
        };
    
        document.querySelector('#chat-message-submit').onclick = function(e) {
            var messageInputDom = document.querySelector('#chat-message-input');
            var message = messageInputDom.value;
            chatSocket.send(JSON.stringify({
                'message': message
            }));
            messageInputDom.value = '';
        };
    </script>
    
    
    <textarea id="chat-log" class='' cols="100" rows="20"></textarea><br/>