-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclient.html
More file actions
111 lines (89 loc) · 2.44 KB
/
client.html
File metadata and controls
111 lines (89 loc) · 2.44 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
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
<html>
<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
<style type="text/css">
.box-header {
background: lightblue;
color: black;
border: 1px solid lightblue;
border-bottom: 0px;
padding: 3px;
font-weight: bold;
font-style: italic;
font-size: 90%;
}
.box-body {
background: lightyellow;
color: black;
font-weight: normal;
border: 1px solid gainsboro;
border-top: 0px;
padding: 3px;
}
#terminal {
height: 85%;
}
</style>
<body>
<div class="box-header">
Server messages
</div>
<div id="message" class="box-body"></div>
<br/>
<div class="box-header">
Terminal screen (your characters are sent to the server and echoed here. ESC stops the server for test purposes.)
</div>
<div id="terminal" class="box-body"></div>
<script>
var socket = io.connect( document.location );
// var cursor = String.fromCharCode( 0x2588 ); // black cursor block
var cursor = "_";
var $cursor = $( "<span cursor=\"1\" style=\"text-decoration:blink;\">" + cursor + "</span>" );
socket.on('connect', function () {
$("#message").html( "client started. Please start the server! Otherwise it will not work." );
$("#terminal").append( $cursor );
});
socket.on('announcement', function(msg) {
$("#message").html( "[announcement] " + msg );
});
socket.on('disconnect', function(msg) {
$("#message").html( "[disconnected] Socket disconnected. Server " + msg );
});
socket.on('user message', function (msg) {
$( "[cursor]" ).remove(); // remove cursor symbol
switch (msg) {
case 13: c = "<br/>";
break;
case 8: c = "";
var buf = $("#terminal").html();
$("#terminal").html( buf.substring( 0, buf.length - 1 ) );
break;
default: c = String.fromCharCode( msg );
}
$('#terminal').html( $('#terminal').html() + c ).append( $cursor );
});
$(function () {
// Prevent backspace in a dropdown from acting as the back button, with jQuery
// http://www.codeproject.com/Articles/35545/Prevent-backspace-in-a-dropdown-from-acting-as-the
$( document ).keydown( function(event) {
if (event.keyCode == 8) {
socket.emit( 'user message', 8 );
return false;
}
});
$( document ).keypress( function(event) {
if (event.keyCode == 8) {
return false;
}
event.preventDefault();
event.stopPropagation();
if ( event.keyCode == 27 ) {
socket.emit( 'user message', 27);
} else {
socket.emit( 'user message', event.which );
}
});
});
</script>
</body>
</html>