-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.html
40 lines (36 loc) · 932 Bytes
/
socket.html
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
<!DOCTYPE html>
<html >
<head>
<meta charset="utf-8">
<title>
Работа с сокетами на python
</title>
</head>
<body>
<div class="message"></div>
<textarea placeholder="Для отправки нажмите Enter"></textarea>
<script>
var socket = new WebSocket("ws://localhost:4000");
socket.onopen = function(){
document.querySelector("textarea").addEventListener('keyup', function(e){
if(e.keyCode === 13){
if(this.value.trim() === ""){
return false;
}
socket.send(this.value.trim());
this.value = "";
}
});
};
socket.onerror = function(){
console.log('Ошибка при подключении');
};
var p = "";
socket.onmessage = function(e){
p = document.createElement("p");
p.innerHTML = e.data;
document.querySelector(".message").appendChild(p);
};
</script>
</body>
</html>