-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathSimpleHtmlClient.cs
More file actions
109 lines (93 loc) · 3.03 KB
/
Copy pathSimpleHtmlClient.cs
File metadata and controls
109 lines (93 loc) · 3.03 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
namespace ChannelWSServer
{
public static class SimpleHtmlClient
{
public const string HTML =
@"<!DOCTYPE html>
<meta charset=""utf-8""/>
<title>WebSocket Echo/Broadcast Client</title>
<script language=""javascript"" type=""text/javascript"">
var wsUri = ""ws://localhost:8080/"";
var output;
var websocket;
function init()
{
output = document.getElementById(""output"");
configWebSocket();
}
function configWebSocket()
{
websocket = new WebSocket(wsUri);
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)
{
emit(""SOCKET OPENED"");
sendTextFrame(""Hello"");
}
function onClose(evt)
{
emit(""SOCKET CLOSED"");
}
function onMessage(evt)
{
emit('<span style=""color:blue;"">RECEIVED: ' + evt.data + '</span>');
}
function onError(evt)
{
emit('<span style=""color:red;"">ERROR: ' + evt.data + '</span>');
}
function sendTextFrame(message)
{
if (websocket.readyState == WebSocket.OPEN)
{
emit(""SENT: "" + message);
websocket.send(message);
}
else
{
emit(""Socket not open, state: "" + websocket.readyState);
}
}
function emit(message)
{
var pre = document.createElement(""p"");
pre.style.wordWrap = ""break-word"";
pre.innerHTML = message;
output.appendChild(pre);
}
function clickSend()
{
var txt = document.getElementById(""newMessage"");
if (txt.value.length > 0)
{
sendTextFrame(txt.value);
txt.value = """";
txt.focus();
}
}
function clickClose()
{
if (websocket.readyState == WebSocket.OPEN)
{
websocket.close();
}
else
{
emit(""Socket not open, state: "" + websocket.readyState);
}
document.getElementById(""sender"").disabled = true;
document.getElementById(""closer"").disabled = true;
document.getElementById(""newMessage"").disabled = true;
}
window.addEventListener(""load"", init, false);
</script>
<h2>Multi-Client WebSocket Echo/Broadcast Test</h2>
<p><input type=""input"" id=""newMessage"" onkeyup=""if(event.key==='Enter') clickSend()""/> <input type=""button"" id=""sender"" value=""Send"" onclick=""clickSend()""/> <input type=""button"" id=""closer"" value=""Disconnect"" onclick=""clickClose()""/>
<div id= ""output""></div>
";
}
}