WebSocket

/views/vadmin/Sockets.html (5.7 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
{% import (
"strings"

"github.com/google/uuid"

"{{{ .Package }}}/app"
"{{{ .Package }}}/app/controller/cutil"
"{{{ .Package }}}/app/lib/websocket"
"{{{ .Package }}}/views/components"
"{{{ .Package }}}/views/layout"
) %}

{% code type Sockets struct {
layout.Basic
Channels []string
Connections []*websocket.Connection
Taps []uuid.UUID
} %}

{% func (p *Sockets) Body(as *app.State, ps *cutil.PageState) %}
<div class="card">
<div class="right"><a href="/admin/sockets/tap"><button>Tap Traffic</button></a></div>
<h3>Activity Taps</h3>
<div class="mt">
{%- if len(p.Taps) == 0 -%}
<em>no active taps</em>
{%- else -%}
<ul>
{%- for _, x := range p.Taps -%}
<li>{%s x.String() %}</li>
{%- endfor -%}
</ul>
{%- endif -%}
</div>
</div>
<div class="card">
<h3>Channels</h3>
<div class="mt">
{%- if len(p.Channels) == 0 -%}
<em>no active channels</em>
{%- else -%}
<ul>
{%- for _, x := range p.Channels -%}
<li><a href="/admin/sockets/chan/{%s x %}">{%s x %}</a></li>
{%- endfor -%}
</ul>
{%- endif -%}
</div>
</div>
<div class="card">
<h3>Active Connections</h3>
<div class="mt">
{%- if len(p.Connections) == 0 -%}
<em>no active connections</em>
{%- else -%}
<div class="overflow full-width">
<table class="expanded">
<thead>
<tr>
<th class="shrink">ID</th>{{{ if .HasUser }}}
<th>User ID</th>{{{ end }}}
<th>Profile Name</th>
<th>Service</th>
<th>Channels</th>
<th></th>
</tr>
</thead>
<tbody>
{%- for _, x := range p.Connections -%}
<tr>
<td class="shrink"><a href="/admin/sockets/conn/{%s x.ID.String() %}">{%s x.ID.String() %}</a></td>{{{ if .HasUser }}}
<td><a href="/admin/db/user/{%s x.Profile.ID.String() %}">{%s x.Profile.ID.String() %}</a></td>{{{ end }}}
<td>{%s x.Profile.String() %}</td>
<td>{%s x.Svc %}</td>
<td>{%s strings.Join(x.Channels, ", ") %}</td>
<td class="shrink"><a href="#modal-conn-{%s x.ID.String() %}"><button type="button">JSON</button></a></td>
</tr>
{%- endfor -%}
</tbody>
</table>
</div>
{%- endif -%}
</div>
</div>
{%- for _, x := range p.Connections -%}
{%= components.JSONModal("conn-" + x.ID.String(), "Connection [" + x.ID.String() + "] JSON", x, 1) %}
{%- endfor -%}
{% endfunc %}

{% code type Channel struct {
layout.Basic
Channel *websocket.Channel
Members []*websocket.Connection
} %}

{% func (p *Channel) Body(as *app.State, ps *cutil.PageState) %}
<div class="card">
<div class="right"><a href="#modal-channel"><button type="button">JSON</button></a></div>
<h3>Channel [{%s p.Channel.Key %}]</h3>
</div>
{%= components.JSONModal("channel", "Channel [" + p.Channel.Key + "] JSON", p.Channel, 1) %}
{%- for _, m := range p.Members -%}
{%= ConnectionCard(m, as, ps) %}
{%- endfor -%}
{% endfunc %}

{% code type Connection struct {
layout.Basic
Connection *websocket.Connection
} %}

{% func (p *Connection) Body(as *app.State, ps *cutil.PageState) %}
{%= ConnectionCard(p.Connection, as, ps) %}
<div class="card">
<h3>Send Message</h3>
<form action="/admin/sockets/conn/{%s p.Connection.ID.String() %}/send" method="post">
<div class="overflow full-width">
<table class="mt expanded">
<tbody>{{{ if .HasUser }}}
{%= components.TableInput("from", "", "From", ps.Profile.ID.String(), 5, "The user this message is from") %}{{{ else }}}
{%= components.TableInput("from", "", "From", p.Connection.ID.String(), 5, "The connection this message is from") %}{{{ end }}}
{%= components.TableInput("channel", "", "Channel", "", 5, "The channel this message is for") %}
{%= components.TableInput("cmd", "", "Command", "", 5, "The command for this message") %}
{%= components.TableTextarea("param", "", "Parameter", 8, "", 5, "JSON object message payload") %}
<tr><td colspan="2"><button type="submit">Send</button></td></tr>
</tbody>
</table>
</div>
</form>
</div>
{% endfunc %}

{% func ConnectionCard(c *websocket.Connection, as *app.State, ps *cutil.PageState) %}
<div class="card">
<div class="right"><a href="#modal-connection-{%s c.ID.String() %}"><button type="button">JSON</button></a></div>
<h3>{%s c.ID.String() %} ({%s c.Username() %})</h3>
<div class="overflow full-width">
<table class="mt expanded">
<tbody>
<tr>
<th>Connection ID</th>
<td>{%s c.ID.String() %}</td>
</tr>{{{ if .HasUser }}}
<tr>
<th>User ID</th>
<td><a href="/admin/db/user/{%s c.Profile.ID.String() %}">{%s c.Profile.ID.String() %}</a></td>
</tr>{{{ end }}}
<tr>
<th>Name</th>
<td>{%s c.Username() %}</td>
</tr>
<tr>
<th>Theme</th>
<td>{%s c.Profile.Theme %}</td>
</tr>
<tr>
<th>Service</th>
<td>{%s c.Svc %}</td>
</tr>
<tr>
<th>Channels</th>
<td>{%s strings.Join(c.Channels, ", ") %}</td>
</tr>
</tbody>
</table>
</div>
</div>
{%= components.JSONModal("connection-" + c.ID.String(), "Connection [" + c.ID.String() + "] JSON", c, 1) %}
{% endfunc %}