DmitrMakeev
commited on
Commit
•
1976238
1
Parent(s):
67f4f3e
Create chat2.html
Browse files- chat2.html +39 -0
chat2.html
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html >
|
3 |
+
<head>
|
4 |
+
<meta charset="utf-8">
|
5 |
+
<title>
|
6 |
+
Работа с сокетами на python
|
7 |
+
</title>
|
8 |
+
</head>
|
9 |
+
<body>
|
10 |
+
<div class="message"></div>
|
11 |
+
<textarea placeholder="Для отправки нажмите Enter"></textarea>
|
12 |
+
<script>
|
13 |
+
var socket = new WebSocket("ws://dmtuit-web-soc-chat.hf.space:7860");
|
14 |
+
socket.onopen = function(){
|
15 |
+
|
16 |
+
document.querySelector("textarea").addEventListener('keyup', function(e){
|
17 |
+
if(e.keyCode === 13){
|
18 |
+
if(this.value.trim() === ""){
|
19 |
+
return false;
|
20 |
+
}
|
21 |
+
socket.send(this.value.trim());
|
22 |
+
this.value = "";
|
23 |
+
}
|
24 |
+
|
25 |
+
});
|
26 |
+
|
27 |
+
};
|
28 |
+
socket.onerror = function(){
|
29 |
+
console.log('Ошибка при подключении');
|
30 |
+
};
|
31 |
+
|
32 |
+
var p = "";
|
33 |
+
socket.onmessage = function(e){
|
34 |
+
p = document.createElement("p");
|
35 |
+
p.innerHTML = e.data;
|
36 |
+
document.querySelector(".message").appendChild(p);
|
37 |
+
};
|
38 |
+
</script>
|
39 |
+
</html>
|