File size: 7,256 Bytes
ca17a67 | 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 | <!--
Copyright (c), Eclipse Foundation, Inc. and its licensors.
All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Distribution License v1.0, which is available at
https://www.eclipse.org/org/documents/edl-v10.php
SPDX-License-Identifier: BSD-3-Clause
-->
<!DOCTYPE html>
<html>
<head>
<title>WebsocketBot</title>
<link rel="stylesheet" type="text/css" href="/css/default.css" />
<script type="text/javascript">
var wsocket; // Websocket connection
var userName; // User's name
var textarea; // Chat area
var wsconsole; // Websocket console area
var userlist; // User list area
/* Connect to the Websocket endpoint
* Set a callback for incoming messages */
function connect() {
textarea = document.getElementById("textarea");
wsconsole = document.getElementById("wsconsole");
userlist = document.getElementById("userlist");
const scheme = location.protocol === "https:" ? "wss" : "ws";
const wsPath = location.pathname.startsWith("/websocketbot/")
? "/websocketbot/websocketbot"
: "/websocketbot";
const url = `${scheme}://${location.host}${wsPath}`;
wsocket = new WebSocket(url);
wsocket.onmessage = onMessage;
document.getElementById("name").focus();
document.getElementById("consolediv").style.visibility = 'hidden';
}
/* Callback function for incoming messages
* evt.data contains the message
* Update the chat area, user's area and Websocket console */
function onMessage(evt) {
var line = "";
/* Parse the message into a JavaScript object */
var msg = JSON.parse(evt.data);
if (msg.type === "chat") {
line = msg.name + ": ";
if (msg.target.length > 0)
line += "@" + msg.target + " ";
line += msg.message + "\n";
/* Update the chat area */
textarea.value += "" + line;
} else if (msg.type === "info") {
line = "[--" + msg.info + "--]\n";
/* Update the chat area */
textarea.value += "" + line;
} else if (msg.type === "users") {
line = "Users:\n";
for (var i=0; i < msg.userlist.length; i++)
line += "-" + msg.userlist[i] + "\n";
/* Update the user list area */
userlist.value = line;
}
textarea.scrollTop = 999999;
/* Update the Websocket console area */
wsconsole.value += "-> " + evt.data + "\n";
wsconsole.scrollTop = 999999;
}
/* Send a join message to the server */
function sendJoin() {
var input = document.getElementById("input");
var name = document.getElementById("name");
var join = document.getElementById("join");
var jsonstr;
if (name.value.length > 0) {
/* Create a message as a JavaScript object */
var joinMsg = {};
joinMsg.type = "join";
joinMsg.name = name.value;
/* Convert the message to JSON */
jsonstr = JSON.stringify(joinMsg);
/* Send the JSON text */
wsocket.send(jsonstr);
/* Disable join controls */
name.disabled = true;
join.disabled = true;
input.disabled = false;
userName = name.value;
/* Update the Websocket console area */
wsconsole.value += "<- " + jsonstr + "\n";
wsconsole.scrollTop = 999999;
}
}
/* Send a chat message to the server (press ENTER on the input area) */
function sendMessage(evt) {
var input = document.getElementById("input");
var jsonstr;
var msgstr;
if (evt.keyCode === 13 && input.value.length > 0) {
/* Create a chat message as a JavaScript object */
var chatMsg = {};
chatMsg.type = "chat";
chatMsg.name = userName;
msgstr = input.value;
chatMsg.target = getTarget(msgstr.replace(/,/g, ""));
chatMsg.message = cleanTarget(msgstr);
chatMsg.message = chatMsg.message.replace(/(\r\n|\n|\r)/gm,"");
/* Convert the object to JSON */
jsonstr = JSON.stringify(chatMsg);
/* Send the message as JSON text */
wsocket.send(jsonstr);
input.value = "";
/* Update the Websocket console area */
wsconsole.value += "<- " + jsonstr + "\n";
wsconsole.scrollTop = 999999;
}
}
/* Send a join message if the user presses ENTER in the name area */
function checkJoin(evt) {
var name = document.getElementById("name");
var input = document.getElementById("input");
if (evt.keyCode === 13 && name.value.length > 0) {
sendJoin();
input.focus();
}
}
/* Get the @User (target) for a message */
function getTarget(str) {
var arr = str.split(" ");
var target = "";
for (var i=0; i<arr.length; i++) {
if (arr[i].charAt(0) === '@') {
target = arr[i].substring(1,arr[i].length);
target = target.replace(/(\r\n|\n|\r)/gm,"");
}
}
return target;
}
/* Remove the @User (target) from a message */
function cleanTarget(str) {
var arr = str.split(" ");
var cleanstr = "";
for (var i=0; i<arr.length; i++) {
if (arr[i].charAt(0) !== '@')
cleanstr += arr[i] + " ";
}
return cleanstr.substring(0,cleanstr.length-1);
}
/* Show or hide the WebSocket console */
function showHideConsole() {
var chkbox = document.getElementById("showhideconsole");
var consolediv = document.getElementById("consolediv");
if (chkbox.checked)
consolediv.style.visibility = 'visible';
else
consolediv.style.visibility = 'hidden';
}
/* Call connect() when the page first loads */
window.addEventListener("load", connect, false);
</script>
</head>
<body>
<h1>WebsocketBot</h1>
Your name: <input id="name" type="text" size="20" maxlength="20" onkeyup="checkJoin(event);"/>
<input type="submit" id="join" value="Join!" onclick="sendJoin();"/><br/>
<textarea id="input" cols="70" rows="1" disabled
onkeyup="sendMessage(event);"></textarea><br/>
<textarea id="textarea" cols="70" rows="20" readonly></textarea>
<textarea id="userlist" cols="20" rows="20" readonly></textarea>
<br/><br/><br/>
<input id="showhideconsole" type="checkbox" onclick="showHideConsole();"/>
Show WebSocket console<br/>
<div id="consolediv"><textarea id="wsconsole" cols="80" rows="8" readonly
style="font-size:8pt;"></textarea></div>
</body>
</html>
|