-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
111 lines (97 loc) · 2.91 KB
/
index.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
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
<!DOCTYPE html>
<html>
<head>
<title>WebSocketChat - Java Server</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
var mouseX = 0;
var mouseY = 0;
$('div#chat-window-title').mousedown(function(event){
$('div#chat-window-wrapper').css({
top: mouseX,
left: mouseY
});
//console.log(mouseX);
});
$('body').mousemove(function(event) {
mouseX = event.pageX;
mouseY = event.pageY;
});
});
var socket = new WebSocket('ws://my.server:1234');
socket.onopen = function() {
$('input[name="message"]').keypress(function(event) {
var code = (event.keyCode ? event.keyCode : event.which);
if(code == 13) {
socket.send($('input[name="message"]').val());
//$('div#chat-window').append(singleMessage($('input[name="message"]').val()));
$('input[name="message"]').val('');
$('#chat-window').scrollTop($('#chat-window').prop('scrollHeight'));
}
});
}
socket.onerror = function (error) {
console.log('WebSocket Error: ' + error);
};
socket.onmessage = function (event) {
var data = event.data;
var nick = data.substring(0, data.indexOf(":") + 1);
var msg = data.substring(data.indexOf(":") + 1);
$('div#chat-window').append(singleMessage(nick, msg));
$('#chat-window').scrollTop($('#chat-window').prop('scrollHeight'));
};
function singleMessage(nick, msg) {
return '<div class="msg">' +
'<div class="nick">' + nick + '</div>' +
'<div class="message">' + msg + '</div>' +
'</div>';
}
</script>
<style type="text/css">
div#chat-window-wrapper {
position: relative;
}
div#chat-window-title {
height: 20px;
}
div#chat-window {
width: 640px;
height: 320px;
border: 1px solid #000;
overflow: auto;
}
div.nick {
font-weight: bold;
display: inline;
}
div.message {
display: inline;
margin-left: 5px;
word-break: break-word;
}
div#chat-message {
border: 1px solid #000;
border-top: none;
width: 640px;
}
div#chat-message input {
border: none;
outline: none;
width: 640px;
}
</style>
</head>
<body>
<div id="chat-window-wrapper">
<div id="chat-window-title">
</div>
<div id="chat-window">
</div>
<div id="chat-message">
<input type="text" name="message">
</div>
</div>
</body>
</html>