-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatClient.dart
261 lines (236 loc) · 6.28 KB
/
ChatClient.dart
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#import('dart:html');
#import('dart:json');
#import('dart:core');
//#import('ui_lib/util/DateUtils.dart');
#source('view/ChatView.dart');
#source('view/View.dart');
#source("view/Participants.dart");
#resource('style.css');
class ChatClient {
WebSocket ws;
bool isConnected = false;
InputElement _nickname;
DivElement _root;
ChatView _chatView;
ChatClient() {
}
// Send nickname to server
sendNick() {
if ( isConnected == true)
{
var name = _nickname.value;
ws.send(JSON.stringify({"cmd": "setnick", "args": name}));
_nickname.value = "";
}
}
void focusOnTab( String title)
{
_chatView.focusOnTab( title);
}
void joinDefaultChannel()
{
joinChannel( "YonderIasi", 1);
joinChannel( "Yonder", 1);
focusOnTab( "Yonder");
}
void joinChannel( String channel, int type)
{
if ( !_chatView.findTab( channel))
{
sendJoinCommand( channel);
_chatView.addTab( channel, type);
}
else
{
focusOnTab( channel);
}
}
void processCommand( String command)
{
command = command.substring( 1);
List<String> parts = command.split( " ");
var cmd = parts[0];
if ( cmd.toLowerCase() == "join")
{
if ( parts.length == 2)
{
var channel = parts[1];
if ( channel != null && channel.length >= 1)
{
joinChannel(channel, 1);
focusOnTab( channel);
}
}
}
}
void sendJoinCommand( String channel)
{
ws.send(JSON.stringify({"cmd": "joinchannel", "args": channel}));
}
// Server sets nickname
setNick(nick)
{
_nickname.value = nick;
}
buildDate( milisec)
{
var date = new Date.fromEpoch(milisec, new TimeZone.local());
var h = date.hours;
var m = date.minutes;
var s = date.seconds;
var string = "$h:$m:$s";
return string;
}
cleanMessage( message)
{
var newMessage = message;
newMessage = newMessage.replaceAll("\n", "");
return newMessage;
}
sendMessage(String channel, String message)
{
message = cleanMessage(message);
if (!message.isEmpty() && message != "\n")
{
ws.send(JSON.stringify({"cmd": "sendmessage", "channel" : channel, "args": message}));
}
}
void run() {
_root = new Element.tag('div');
document.body.nodes.add(_root);
_chatView = new ChatView( _root, this);
_chatView.ready();
_nickname = document.query("#nickname");
_nickname.on.keyPress.add((key) {
if (key.charCode == 13) { // Enter
sendNick();
}
});
ws = new WebSocket("ws://192.168.1.123:1337");
//ws = new WebSocket("ws://127.0.0.1:1337");
ws.on.open.add((a) {
print("open $a");
isConnected = true;
print("You are connected.");
//_statusBar.innerHTML = "You are connected.";
});
ws.on.close.add((c) {
print("close $c");
isConnected = false;
print("You are not connected.");
//_statusBar.innerHTML = "You are not connected.";
});
ws.on.message.add((m) {
var jdata = JSON.parse(m.data);
var cmd = jdata["cmd"].toLowerCase();
print( "Data : $jdata \n");
if (cmd == "newmessage")
{
var nickname = jdata["nickname"];
var message = jdata["message"];
var time = jdata["time"];
var channel = jdata["channel"];
var date = buildDate(time);
if ( nickname != null)
{
displayMessage( channel, "($date) $nickname > $message");
}
else
{
displayMessage( channel, "($date) $message");
}
}
else if ( cmd == "history")
{
var channel = jdata["channel"];
var data = jdata["data"];
for( final x in data)
{
var time = x["time"];
var author = x["author"];
var text = x["text"];
var line;
var date = buildDate(time);
if ( author != null)
{
line = "($date) $author > $text";
}
else
{
line = "($date) $text";
}
print( line);
displayMessage( channel, line);
}
}
else if ( cmd == "invalidnickname")
{
var message = jdata["message"];
unhideNickPanel(message);
}
else if ( cmd == "nicknameaccepted")
{
hideNickPanel();
joinDefaultChannel();
}
else if ( cmd == "userjoin")
{
var message = jdata["message"];
var nickname = jdata["nickname"];
var time = jdata["time"];
var channel = jdata["channel"];
var date = buildDate(time);
displayMessage( channel, "($date) $message");
addParticipant( channel, nickname);
}
else if (cmd == "userleft")
{
var nickname = jdata["nickname"];
var message = jdata["message"];
var time = jdata["time"];
var channel = jdata["channel"];
var date = buildDate(time);
displayMessage( channel, "($date) $message");
removeParticipant( channel, nickname);
}
else if ( cmd == "participants")
{
var channel = jdata["channel"];
var participants = jdata["participants"];
_chatView.setParticipants( channel, participants);
}
});
}
void hideNickPanel()
{
document.query('#mainInput').style.zIndex = '-1';
document.query('#mainInput').style.visibility = 'hidden';
_root.hidden = false;
document.query('#errorDiv').innerHTML = "";
}
void unhideNickPanel( message)
{
document.query('#mainInput').style.zIndex = '1';
document.query('#mainInput').style.visibility = 'visible';
_root.hidden = true;
if ( message != null)
{
document.query('#errorDiv').innerHTML = message;
}
}
void displayMessage(String channel, String message)
{
_chatView.displayMessage( channel, message);
}
void addParticipant( String channel, String participant)
{
_chatView.addParticipant( channel, participant);
}
void removeParticipant( String channel, String participant)
{
_chatView.removeParticipant( channel, participant);
}
}
void main() {
new ChatClient().run();
}