-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebSocket.html
More file actions
58 lines (58 loc) · 1.73 KB
/
WebSocket.html
File metadata and controls
58 lines (58 loc) · 1.73 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WebSocket</title>
<link rel="stylesheet" href="css/WebSocket.css">
</head>
<body>
<div id="chat_wrapper">
<h2>Awesome Help!</h2>
<form id="nick_form" action="#" method="post">
<p>
<label>Nickname
<input id="nickname" type="text" value="GuestUser">
</label>
<input type="submit" value="Change">
</p>
</form>
<div id="chat">connecting</div>
<form id="chat_form" action="#" method="post">
<p>
<label>Message
<input id="message" type="text">
</label>
<input type="submit" value="Send">
</p>
</form>
</div>
<script src="https://code.jquery.com/jquery-2.0.0.min.js"></script>
<script>
var setupChat=function() {
var webSocket = new WebSocket('ws://123.206.79.46:60001/');
webSocket.onopen = function (event) {
$('#chat').append('<br>Connected to the server');
};
webSocket.onmessage = function (event) {
$('#chat').append("<br>" + event.data);
$('#chat').animate({scrollTop: $('#chat').height()});
};
webSocket.onclose=function (evnet) {
$("#chat").append('<br>Connection closed');
};
$("form#chat_form").submit(function(e){
e.preventDefault();
var textfield=$("#message");
webSocket.send(textfield.val());
textfield.val("");
});
$("form#nick_form").submit(function(e){
e.preventDefault();
var textfield=$("#nickname");
var textfield=$("/nick"+textfield.val());
});
};
setupChat();
</script>
</body>
</html>