-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJASON.html
More file actions
184 lines (153 loc) · 4.41 KB
/
JASON.html
File metadata and controls
184 lines (153 loc) · 4.41 KB
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
<!--
JASON.html
@brief
JASON is a simple, web-based tool for creating json objects, sending it via websocket
and displaying the response.
JASON is using Jos de Jong's jsoneditor: https://github.com/josdejong/jsoneditor
@license
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
Copyright (c) 2019 HuemerGroup
@author Oskar Kruschitz https://github.com/hiiamok
@version 1.0.2
@date 2019-05-14
-->
<html>
<head>
<meta charset="UTF-8">
<title>JASON - Json+Websocket Communication Interface</title>
<link href="jsoneditor/dist/jsoneditor.css" rel="stylesheet" type="text/css">
<script src="jsoneditor/dist/jsoneditor.js"></script>
<link href="theme.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="statusbar">
<input type="text" id="header" value="J A S O N" readonly />
<input type="button" id="connectBtn" value="CONNECT" onclick="connect()" />
<input type="button" id="disconnectBtn" value="DISCONNECT" onclick="closeSocket()" disabled="true" />
<input type="text" id="address" value="ws://localhost:80" />
<input type="button" id="sendBtn" value="SEND" onclick="send()" disabled="true" style="width:10%;" />
<input type="button" id="clearBtn" value="CLEAR" onclick="clearOutput()" disabled="true" />
</div>
<div id="container">
<div id="jsonInput"></div>
<div id="output"></div>
</div>
</body>
<script type="text/javascript">
var webSocket;
var output = document.getElementById("output");
var connectBtn = document.getElementById("connectBtn");
var disconnectBtn = document.getElementById("disconnectBtn");
var sendBtn = document.getElementById("sendBtn");
var address = document.getElementById("address");
var clearBtn = document.getElementById("clearBtn");
/**
*
* Connect Websocket and initialize Handler
*
* */
function connect() {
if (webSocket !== undefined &&
webSocket.readyState !== WebSocket.CLOSED) {
return;
}
webSocket = new WebSocket(address.value);
webSocket.onopen = function (event) {
updateOutput("Connected");
connectBtn.disabled = true;
sendBtn.disabled = false;
disconnectBtn.disabled = false;
address.readOnly = true;
address.style.color = 'lightgreen';
};
webSocket.onmessage = function (event) {
updateOutput(JSON.parse(event.data));
};
webSocket.onclose = function (event) {
updateOutput("Connection Closed");
connectBtn.disabled = false;
sendBtn.disabled = true;
disconnectBtn.disabled = true;
address.readOnly = false;
address.style.color = 'indianred';
};
}
/**
*
* Send Websocket Message
*
* */
function send() {
var json = inputEditor.get();
webSocket.send(JSON.stringify(json));
}
/**
*
* Close Socket Connection
*
* */
function closeSocket() {
webSocket.close();
}
/**
*
* Add Result Message
*
* */
function updateOutput(data) {
var newOutput = document.createElement('div');
if (typeof data == "string") {
newOutput.innerHTML += data;
newOutput.setAttribute("class", "action");
output.insertBefore(newOutput, output.firstChild);
}
output.insertBefore(newOutput, output.firstChild);
if (typeof data != "string") {
newOutput.setAttribute("class", "reply");
output.insertBefore(newOutput, output.firstChild);
var o = options;
o.modes = ["view", "code"];
o.onEditable = function (node) {
if (!node.path) {
return false;
}
};
var editor = new JSONEditor(newOutput, options, data);
editor.expandAll();
}
clearBtn.disabled = false;
}
function clearOutput() {
output.innerHTML = '';
clearBtn.disabled = true;
}
var container = document.getElementById('jsonInput');
var options = {
modes: ['tree', 'code'],
templates: []
};
/**
*
* Default json object
*
* */
var json = {
'timestamp': new Date().toISOString(),
'key': 'value',
'object': {
'sub_key_1': 'value_1',
'sub_key_2': 'value_2'
}
};
var inputEditor = new JSONEditor(container, options, json);
inputEditor.expandAll();
</script>
</html>