-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
222 lines (202 loc) · 7.18 KB
/
index.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<html>
<head>
<title>Serverless Chat</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<script>
//window.apiBaseUrl = 'http://localhost:7071';
window.apiBaseUrl = 'https://jixinfunctest.azurewebsites.net';
</script>
<!-- Microsoft Clarity: https://clarity.microsoft.com/projects/view/b1b5l8fxnv/settings#setup -->
<script type="text/javascript">
(function(c,l,a,r,i,t,y){
c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
})(window, document, "clarity", "script", "b1b5l8fxnv");
</script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XLRRRNN2FR"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XLRRRNN2FR');
</script>
<style>
.slide-fade-enter-active, .slide-fade-leave-active {
transition: all 1s ease;
}
.slide-fade-enter, .slide-fade-leave-to {
height: 0px;
overflow-y: hidden;
opacity: 0;
}
</style>
</head>
<body>
<p> </p>
<div id="app" class="container">
<h3>Serverless chat</h3>
<div class="row" v-if="ready">
<div class="signalr-demo col-sm">
<hr />
<div id='groupchecked'>
<input type="checkbox" id="checkbox" v-model="checked">
<label for="checkbox">Send To Default Group: {{ this.defaultgroup }}</label>
</div>
<form v-on:submit.prevent="sendNewMessage(checked)">
<input type="text" v-model="newMessage" id="message-box" class="form-control" placeholder="Type message here..." />
</form>
</div>
</div>
<div class="row" v-if="!ready">
<div class="col-sm">
<div>Loading...</div>
</div>
</div>
<div v-if="ready">
<transition-group name="slide-fade" tag="div">
<div class="row" v-for="message in messages" v-bind:key="message.id">
<div class="col-sm">
<hr />
<div>
<div style="display: inline-block; padding-left: 12px;">
<div>
<a href="#" v-on:click.prevent="sendPrivateMessage(message.sender)">
<span class="text-info small"><strong>{{ message.sender }}</strong></span>
</a>
<a href="#" v-on:click.prevent="addToGroup(message.sender)">
<span class="badge badge-primary">AddGroup</small>
</a>
<a href="#" v-on:click.prevent="removeFromGroup(message.sender)">
<span class="badge badge-primary">RemoveGroup</small>
</a>
<span v-if="message.isPrivate" class="badge badge-secondary">private message</small>
</div>
<div>
{{ message.text }}
</div>
</div>
</div>
</div>
</div>
</div>
</transition-group>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@aspnet/[email protected]/dist/browser/signalr.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/axios.min.js"></script>
<script>
const data = {
username: '',
defaultgroup: 'AzureSignalR',
checked: false,
newMessage: '',
messages: [],
ready: false
};
const app = new Vue({
el: '#app',
data: data,
methods: {
sendNewMessage: function (isToGroup) {
if(isToGroup) {
sendMessage(this.username, null, this.defaultgroup, this.newMessage);
}
else {
sendMessage(this.username, null, null, this.newMessage);
}
this.newMessage = '';
},
sendPrivateMessage: function (recipient) {
const messageText = prompt('Send private message to ' + recipient);
if (messageText) {
sendMessage(this.username, recipient, null, messageText);
}
},
addToGroup: function (recipient) {
var r = confirm('Add user ' + recipient + ' to group: ' + this.defaultgroup);
if(r) {
addGroup(this.username, recipient, this.defaultgroup);
}
},
removeFromGroup: function (recipient) {
var r = confirm('Remove user ' + recipient + ' from group: ' + this.defaultgroup);
if(r) {
removeGroup(this.username, recipient, this.defaultgroup);
}
}
}
});
const apiBaseUrl = prompt('Enter the Azure Function app base URL', window.apiBaseUrl);
data.username = prompt("Enter your username");
if (!data.username) {
alert("No username entered. Reload page and try again.");
throw "No username entered";
}
getConnectionInfo().then(info => {
// make compatible with old and new SignalRConnectionInfo
info.accessToken = info.accessToken || info.accessKey;
info.url = info.url || info.endpoint;
data.ready = true;
const options = {
accessTokenFactory: () => info.accessToken
};
const connection = new signalR.HubConnectionBuilder()
.withUrl(info.url, options)
.configureLogging(signalR.LogLevel.Information)
.build();
connection.on('newMessage', newMessage);
connection.onclose(() => console.log('disconnected'));
console.log('connecting...');
connection.start()
.then(() => console.log('connected!'))
.catch(console.error);
}).catch(alert);
function getAxiosConfig() {
const config = {
headers: {'x-ms-signalr-userid': data.username}
};
return config;
}
function getConnectionInfo() {
return axios.post(`${apiBaseUrl}/api/negotiate`, null , getAxiosConfig())
.then(resp => resp.data);
}
function sendMessage(sender, recipient, groupname, messageText) {
return axios.post(`${apiBaseUrl}/api/messages`, {
recipient: recipient,
isPrivate: recipient != null,
groupname: groupname,
sender: sender,
text: messageText
}, getAxiosConfig()).then(resp => resp.data);
}
function addGroup(sender, recipient, groupName) {
return axios.post(`${apiBaseUrl}/api/group`, {
action: "add",
recipient: recipient,
groupname: groupName
}, getAxiosConfig()).then(resp => {
if(resp.status == 200) {
confirm("Add Successfully")
}});
}
function removeGroup(sender, recipient, groupName) {
return axios.post(`${apiBaseUrl}/api/group`, {
action: "remove",
recipient: recipient,
groupname: groupName
}, getAxiosConfig()).then(resp => {
if(resp.status == 200) {
confirm("Remove Successfully")
}});
}
let counter = 0;
function newMessage(message) {
message.id = counter++; // vue transitions need an id
data.messages.unshift(message);
}
</script>
</body>
</html>