Subversion Repository Public Repository

Nextrek

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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<!doctype html>
<html>
  <head>
    <title>Socket.IO</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      .form1 { background: #000; padding: 2px; position: fixed; bottom: 0; width: 100%; }
      .form1 input { border: 0; padding: 8px; width: 90%; margin-right: .5%; }
      .form1 button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 8px; }
	  .form2 { background: #000; padding: 2px; position: fixed; bottom: 32px; width: 100%; }
      .form2 input { border: 0; padding: 8px; width: 90%; margin-right: .5%; }
      .form2 button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 8px; }
	  .form3 { background: #000; padding: 2px; position: fixed; bottom: 64px; width: 100%; }
      .form3 input { border: 0; padding: 8px; width: 90%; margin-right: .5%; }
      .form3 button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 8px; }
	  .username { background: #000; padding: 2px; position: fixed; bottom: 96px; width: 100%; }
      .username input { border: 0; padding: 8px; width: 90%; margin-right: .5%; }
      .username button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 8px; }
	  .online { background: #000; padding: 2px; position: fixed; bottom: 128px; width: 100%; }
      .online div { border: 0; padding: 8px; width: 90%; margin-right: .5%; background-color: white;}
      .sendImageContainer { color: white; background: #000; padding: 2px; position: fixed; bottom: 160px; width: 100%; }
      .sendImageContainer input { border: 0; padding: 8px; width: auto; margin-right: .5%; }
      .sendImageContainer button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 8px; }
	  .imgPreview {display: none; height: 38px; margin-bottom: -15px; padding-left: 10px; padding-right: 50px;}
	  .image-link img {border: 2px solid blue; border-radius: 5px; cursor: pointer;}
	  
	  
	  
	  .msg1, .chat_message1 {font-size: 10px;}
	  .msg2, .chat_message2 {font-size: 12px;}
	  .msg3, .chat_message3 {font-size: 14px;}
	  .private_message {font-size: 14px; color: purple;}
	  .private_message_sent {font-size: 14px; color: green;}
	  .chname, .user {font-size: 13px; color: blue;}
	  .broadcast, .nxt-broadcast {font-size: 16px; font-weight: bold;}
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
	  
	  .online div.user-elem {background-color: #6755f7; color: #f3bc42; padding: 3px; cursor: pointer; display: inline; border-radius: 3px;}
    </style>
	<script type="text/javascript" src="/js/socket.io.dev.js"></script>
	<script type="text/javascript" src="/js/jquery-3.3.1.min.js"></script>
	<script>
		var socket = undefined;
		
		
		function parseMessage(msg){
			var json = JSON.parse(msg);
			var toWrite = "";
			switch (json.contentType){
				case "text/plain":
					toWrite = parseTextMessage(json);
					break;
				case "System":
					toWrite = parseSystemMessage(json);
					break;
				case "user":
					toWrite = parseUserMessage(json);
					break;
				/* add other parse types*/
				default:
					toWrite = parseTextMessage(json);
					break;
			}
			toWrite.messageType = json.messageType;
			return toWrite;
		}
		
		function parseImage(msg){
			var json = JSON.parse(msg);
			return json;
		}
		
		function parseTextMessage(json){
			var toWrite = {};
			toWrite.data = "";
			toWrite.data += ":: "+new Date(json.ts) + " - ";
			toWrite.data += json.user + ": ";
			toWrite.data += json.content;
			return toWrite;
		}
		
		function parseSystemMessage(json){
			var toWrite = {};
			toWrite.data = "";
			toWrite.data += "!! "+new Date(json.ts) + " - ";
			toWrite.data += json.user + ": ";
			if (json.content === "connected"){
				toWrite.data += " has connected";
			}else if (json.content === "disconnected"){
				toWrite.data += " left the room!";
			}
			return toWrite;
		}
		
		function parseUserMessage(json){
			var toWrite = {};
			toWrite.data = "";
			if (json.messageType == "invalid change name"){
				toWrite.data += "CN: "+new Date(json.ts) + " - ";
				toWrite.data += json.user + ": ";
				toWrite.data += " tried to changed name to: " + json.content + ". This is invalid (userName already exists or the specified name conatains invalid characters ('/','@',':'))";
			}else{
				toWrite.data += "CN: "+new Date(json.ts) + " - ";
				toWrite.data += json.user + ": ";
				toWrite.data += " has changed name to: " + json.content;
			}
			
			return toWrite;
		}
		
		$(document).on('change','#imageElem' , function(){ 
			var filesSelected = document.getElementById("imageElem").files;
			if (filesSelected.length > 0){
				var fileToLoad = filesSelected[0];
		 
				var fileReader = new FileReader();
		 
				fileReader.onload = function(fileLoadedEvent) 
				{
					$(".imgPreview").attr("src", fileLoadedEvent.target.result);
					$(".imgPreview").show();
				};
		 
				fileReader.readAsDataURL(fileToLoad);
			}
		});
		
		$(function () {
			socket = io();
			$('.form1').submit(function(e){
				e.preventDefault();
				if ($('#m1').val().trim() !== ""){
					var msg = $('#m1').val();
					var json = {
						content: msg,
						contentType: "text/plain",
						messageType: 'chat message1',
						ts: new Date().getTime(),
						user: "self"
					}
					socket.emit('chat message1', JSON.stringify(json));
					$('#m1').val('');
				}
				
				if (json.content.indexOf("/@") == 0 && json.content.indexOf(":")>-1) {
					var privateMess = json.content.substring(json.content.indexOf(":")+1);
					var privateDest = json.content.substring(2, json.content.indexOf(":"));
					$('#messages').append($('<li class="private_message_sent"></li>').text("Private message to " + privateDest + ":" + privateMess));
				}
				
				return false;
			});
			$('.form2').submit(function(e){
				e.preventDefault();
				if ($('#m2').val().trim() !== ""){
					var msg = $('#m2').val();
					var json = {
						content: msg,
						contentType: "text/plain",
						messageType: 'chat message2',
						ts: new Date().getTime(),
						user: "self"
					}
					socket.emit('chat message2', JSON.stringify(json));
					$('#m2').val('');
				}
				
				if (json.content.indexOf("/@") == 0 && json.content.indexOf(":")>-1) {
					var privateMess = json.content.substring(json.content.indexOf(":")+1);
					var privateDest = json.content.substring(2, json.content.indexOf(":"));
					$('#messages').append($('<li class="private_message_sent"></li>').text("Private message to " + privateDest + ":" + privateMess));
				}
				
				return false;
			});
			$('.form3').submit(function(e){
				e.preventDefault();
				if ($('#m3').val().trim() !== ""){
					var msg = $('#m3').val();
					var json = {
						content: msg,
						contentType: "text/plain",
						messageType: 'chat message3',
						ts: new Date().getTime(),
						user: "self"
					}
					socket.emit('chat message3', JSON.stringify(json));
					$('#m3').val('');
				}
				
				if (json.content.indexOf("/@") == 0 && json.content.indexOf(":")>-1) {
					var privateMess = json.content.substring(json.content.indexOf(":")+1);
					var privateDest = json.content.substring(2, json.content.indexOf(":"));
					$('#messages').append($('<li class="private_message_sent"></li>').text("Private message to " + privateDest + ":" + privateMess));
				}
				
				return false;
			});
			$('.username').submit(function(e){
				e.preventDefault();
				if ($('#un').val().trim() !== ""){
					var json = {
						content: $('#un').val(),
						contentType: "user",
						messageType: 'change name',
						ts: new Date().getTime(),
						user: "self"
					}
					socket.emit('change name', JSON.stringify(json));
					$('#un').val('');
				}
				return false;
			});
			$(".sendImageContainer").submit(function(e){
				e.preventDefault();
				var data = $(".imgPreview").attr("src");
				if (data != undefined && data != ""){
					var json = {
						content: data,
						contentType: "image",
						messageType: 'image-base64',
						ts: new Date().getTime(),
						user: "self"
					}
					socket.emit('image-base64', JSON.stringify(json));
					$('#imageElem').val('');
					$(".imgPreview").attr("src", "");
					$(".imgPreview").hide();
				}
				
				
				return false;
			
			});
			
			
			
			
			socket.on('chat message1', function(msg){
				var toWrite = parseMessage(msg);
				$('#messages').append($('<li class="'+toWrite.messageType.replace(/ /g, "_")+'">').text(toWrite.data));
			});
			socket.on('chat message2', function(msg){
				var toWrite = parseMessage(msg);
				$('#messages').append($('<li class="'+toWrite.messageType.replace(/ /g, "_")+'">').text(toWrite.data));
			});
			socket.on('chat message3', function(msg){
				var toWrite = parseMessage(msg);
				$('#messages').append($('<li class="'+toWrite.messageType.replace(/ /g, "_")+'">').text(toWrite.data));
			});
			socket.on('private message', function(msg){
				var toWrite = parseMessage(msg);
				$('#messages').append($('<li class="'+toWrite.messageType.replace(/ /g, "_")+'">').text(toWrite.data));
			});
			socket.on('change name', function(msg){
				var toWrite = parseMessage(msg);
				$('#messages').append($('<li class="'+toWrite.messageType.replace(/ /g, "_")+'">').text(toWrite.data));
			});
			socket.on('invalid change name', function(msg){
				var toWrite = parseMessage(msg);
				$('#messages').append($('<li class="'+toWrite.messageType.replace(/ /g, "_")+'">').text(toWrite.data));
			});
			
			socket.on('nxt-broadcast', function(msg){
				var toWrite = parseMessage(msg);
				$('#messages').append($('<li class="'+toWrite.messageType.replace(/ /g, "_")+'">').text(toWrite.data));
			});
			
			
			socket.on("nxt-online-users", function(msg){
				
				var users = JSON.parse(msg);
				$("#online-users").empty();
				for (elem in users){
					var item = $("<div class='user-elem' nxt-userid='"+users[elem].user+"' nxt-username='"+users[elem].name+"'> "+users[elem].user + " " + users[elem].name +" </div>");
					item.click(function(){
						var value = "/@"+$(this).attr("nxt-username") + ": ";
						$('#m3, #m2, #m1').val(value);
					});
					$("#online-users").append(item);
				}
			
			});
			
			socket.on("image-link", function(msg){
				var toWrite = parseImage(msg);
				var container = $('<li full-source="'+toWrite.contentFull+'" class="'+toWrite.messageType.replace(/ /g, "_")+'">');
				$('#messages').append(container);
				container.append($("<p>"+toWrite.user+":</p>"));
				var image = $("<img src='"+toWrite.content+"'/>");
				container.append(image);
				image.click(function(){
					window.open($(this).parent().attr("full-source"));
				})
			});
			
		  });
	</script>
  </head>
  <body>
    <ul id="messages"></ul>
    <form class="form1" action="">
      <input id="m1" autocomplete="off" /><button>Send 1</button>
    </form>
	<form class="form2" action="">
      <input id="m2" autocomplete="off" /><button>Send 2</button>
    </form>
	<form class="form3" action="">
      <input id="m3" autocomplete="off" /><button>Send 3</button>
    </form>
	<form class="username" action="">
      <input id="un" autocomplete="off" /><button>Change name</button>
    </form>
	<form class="online" action="">
      <div id="online-users"></div>
    </form>
	<form class="sendImageContainer" action="">
      Choose file to upload <input type="file" id="imageElem" name="image" accept="image/png, image/jpeg" /> <img class="imgPreview"></img>
	  <button>Send Image</button>
    </form>
	
  </body>
</html>

Commits for Nextrek/socketIO/public/index.html

Diff revisions: vs.
Revision Author Commited Message
1116 MOliva picture MOliva Tue 25 Sep, 2018 14:16:19 +0000