This repository has been archived by the owner on Feb 1, 2019. It is now read-only.
forked from dpallot/simple-websocket-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket.html
147 lines (126 loc) · 3.47 KB
/
websocket.html
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
<!DOCTYPE html>
<meta charset="utf-8" />
<title>WebSocket Test</title>
<script language="javascript" type="text/javascript">
// ansi color -> html; https://github.com/mmalecki/ansispan
var ansispan = function (str) {
Object.keys(ansispan.foregroundColors).forEach(function (ansi) {
var span = '<span style="color: ' + ansispan.foregroundColors[ansi] + '">';
str = str.replace(
new RegExp('\033\\[' + ansi + 'm', 'g'),
span
).replace(
new RegExp('\033\\[1;' + ansi + 'm', 'g'),
span
);
});
return restoreURI(str.replace(/\033\[0m/g, '</span>'));
};
ansispan.foregroundColors = {
'36': 'cyan',
'90': 'gray',
'92': 'mediumspringgreen',
'93': 'yellow',
'91': 'red',
'95': 'darkred'
};
// replace start
var tagsToReplace = {
'&': '&',
'<': '<',
'>': '>'
};
function replaceTag(tag) {
return tagsToReplace[tag] || tag;
}
function safe_tags_replace(str) {
return str.replace(/[&<>]/g, replaceTag);
}
function restoreURI(str) {
return str.includes('<span style="color: cyan">majordomo</span>') ? decodeURI(str) : str;
}
// replace end
function doConnect()
{
websocket = new WebSocket("ws://" + document.myform.url.value + ":7999/");
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
websocket.onmessage = function(evt) { onMessage(evt) };
websocket.onerror = function(evt) { onError(evt) };
}
function onOpen(evt)
{
writeToScreenMsg("connected");
document.myform.connectButton.value = "Disconnect";
websocket.send(document.myform.ws_token.value);
websocket.send('remote_log');
}
function onClose(evt)
{
writeToScreenMsg("disconnected");
document.myform.connectButton.value = "Connect";
}
function onMessage(evt)
{
writeToScreenLog(evt.data);
}
function onError(evt)
{
writeToScreenMsg('error: ' + evt.data);
websocket.close();
document.myform.connectButton.value = "Disconnect";
}
function writeToScreenMsg(message)
{
writeToScreen('<span style="color: darkorange">' + message + '</span>');
}
function writeToScreenLog(message)
{
writeToScreen(ansispan(safe_tags_replace(message)));
}
function writeToScreen(message)
{
remote_log.innerHTML += message + "<br>";
remote_log.scrollTop = remote_log.scrollHeight;
}
function sendText() {
doSend( document.myform.inputtext.value );
}
function clearText() {
document.getElementById("outputtext").innerHTML = "";
}
function doDisconnect() {
websocket.close();
}
function doConnectDisconnect() {
(document.myform.connectButton.value == "Connect") ? doConnect() : doDisconnect()
}
</script>
<style>
.outputtext {
background-color: black;
color: aliceblue;
overflow-y: auto;
overflow-x: hidden;
width: 50%;
height: 400px;
font-family: monospace;
border-color: black;
padding: 2px;
border-width: thick;
border-style: solid;
}
</style>
<form name="myform">
<p>
token: <input type="text" name="ws_token" size="20" value="token_is_unset"></input>
ip: <input type="text" name="url" size="20" value="127.0.0.1"></input>
<input type="button" name=clearButton value="Clear" onClick="clearText();">
<input type="button" name=connectButton value="Connect" onClick="doConnectDisconnect();">
</p>
</form>
<div id="outputtext" class="outputtext"></div>
<script language="javascript" type="text/javascript">
var remote_log = document.getElementById("outputtext");
</script>
</html>