-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.js
178 lines (164 loc) · 4.63 KB
/
client.js
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* global io */
function Client ( iId, usId ) {
var input,
userStatus,
socket,
curHeight,
listeners = { };
function BoxQueue ( boxId, overrideId ) {
this.box = document.getElementById( overrideId || boxId );
listeners[boxId] = this;
this.lines = [ ];
var b = this;
var resizer = function () {
b.box.style.height = ( window.innerHeight - 30 ) + "px";
};
resizer();
window.addEventListener( "resize", resizer );
}
BoxQueue.prototype.enq = function ( data ) {
var text = "";
if ( typeof ( data ) === "string" )
text = data;
else {
if ( data.message === "say" )
data.payload.unshift( ":" );
else if ( data.message === "tell" )
data.payload.unshift( ": <whisper>" );
else
data.payload.unshift( data.message );
if ( data.fromId !== "server" )
data.payload.unshift( data.fromId );
text = data.payload.join( " " );
}
text = text.replace( /\t/g, " " );
this.lines = this.lines.concat( text.split( "\n" ) );
};
BoxQueue.prototype.next = function () {
if ( this.lines.length > 0 ) {
var elem = document.createElement( "div" );
var line = this.lines.shift();
elem.className = "line";
this.box.appendChild( elem );
elem.innerHTML = line;
this.box.scrollTop = this.box.scrollHeight;
}
};
function display () {
for ( var id in listeners ) {
listeners[id].next();
}
}
setInterval( display, 50 );
var commandQueue = [],
commandIndex = -1;
function submitCommand ( evt ) {
if ( evt.keyCode === 13 ) {
enterCommand();
evt.preventDefault();
return false;
}
return true;
}
function enterCommand () {
var val = input.value.trim();
try {
var type = "cmd";
switch ( input.placeholder )
{
case "<enter name>":
type = "name";
break;
case "<enter password>":
type = "password";
break
default:
type = "cmd";
break;
}
if(type === "cmd") {
commandQueue.push(val);
commandIndex = commandQueue.length;
}
socket.emit( type, val );
input.value = "";
input.focus();
}
catch ( exp ) {
console.log( exp.message );
}
}
function moveCommandQueue(di){
commandIndex += di;
input.value = commandQueue[commandIndex];
input.selectionStart = input.selectionEnd = input.value.length;
}
function keyDown(evt){
if(input.placeholder === "<enter command>") {
if ( evt.keyCode === 38) {
if(commandIndex > 0 && commandQueue.length > 0){
moveCommandQueue(-1);
}
}
else if ( evt.keyCode === 40) {
if(commandIndex < commandQueue.length){
moveCommandQueue(+1);
}
}
}
}
this.focus = function(){
input.focus();
};
try {
input = document.getElementById( iId );
input.addEventListener( "keypress", submitCommand, false );
input.addEventListener( "keydown", keyDown, false );
userStatus = document.getElementById( usId );
curHeight = window.innerHeight;
new BoxQueue( "news" );
var protocol = location.protocol.replace("http", "ws"),
serverPath = protocol + "//" + location.hostname;
socket = io( serverPath );
socket.on( "news", function ( data ) {
console.log( JSON.stringify( data ) );
listeners[data.type].enq( data );
} );
socket.on( "userStatus", function ( data ) {
userStatus.innerHTML = data;
} );
socket.on( "connect", function () {
listeners.news.enq( "Connected." );
listeners.news.enq( "Enter name." );
input.placeholder = "<enter name>";
input.type = "text";
input.enabled = true;
} );
socket.on( "bad name", function ( data ) {
listeners.news.enq( data );
} );
socket.on( "good name", function ( data ) {
listeners.news.enq( data );
listeners.news.enq( "Enter password." );
input.placeholder = "<enter password>";
input.type = "password";
} );
socket.on( "bad password", function ( data ) {
listeners.news.enq( "Incorrect password. Enter password" );
} );
socket.on( "good password", function ( data ) {
listeners.news.enq( "Password accepted." );
input.placeholder = "<enter command>";
input.type = "text";
} );
socket.on( "disconnect", function () {
listeners.news.enq( "Disconnected." );
input.placeholder = "<disconnected>";
input.enabled = false;
userStatus.innerHTML = "";
} );
}
catch ( exp ) {
console.log( exp.message );
}
}