-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update GitHubChat sample to use net7 (#245)
* update GitHubChat sample to use net7 * Remove not needed package
- Loading branch information
Showing
13 changed files
with
403 additions
and
7,270 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,163 +1,164 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" /> | ||
<meta name="viewport" content="width=device-width"> | ||
<meta http-equiv="Pragma" content="no-cache" /> | ||
<meta http-equiv="Expires" content="0" /> | ||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet" /> | ||
<link href="css/site.css" rel="stylesheet" /> | ||
<title>Azure SignalR Group Chat</title> | ||
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" /> | ||
<meta name="viewport" content="width=device-width"> | ||
<meta http-equiv="Pragma" content="no-cache" /> | ||
<meta http-equiv="Expires" content="0" /> | ||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet" /> | ||
<link href="css/site.css" rel="stylesheet" /> | ||
<title>Azure SignalR Group Chat</title> | ||
</head> | ||
|
||
<body> | ||
<h2 class="text-center" style="margin-top: 0; padding-top: 30px; padding-bottom: 30px;">Azure SignalR Group Chat</h2> | ||
<div class="container" style="height: calc(100% - 110px);"> | ||
<div id="messages" style="background-color: whitesmoke; "></div> | ||
<div style="width: 100%; border-left-style: ridge; border-right-style: ridge;"> | ||
<textarea id="message" | ||
style="width: 100%; padding: 5px 10px; border-style: hidden;" | ||
placeholder="Type message and press Enter to send..."></textarea> | ||
</div> | ||
<div style="overflow: auto; border-style: ridge; border-top-style: hidden;"> | ||
<button class="btn-warning pull-right" id="echo">Echo</button> | ||
<button class="btn-success pull-right" id="sendmessage">Send</button> | ||
</div> | ||
<h2 class="text-center" style="margin-top: 0; padding-top: 30px; padding-bottom: 30px;">Azure SignalR Group Chat</h2> | ||
<div class="container" style="height: calc(100% - 110px);"> | ||
<div id="messages" style="background-color: whitesmoke; "></div> | ||
<div style="width: 100%; border-left-style: ridge; border-right-style: ridge;"> | ||
<textarea id="message" style="width: 100%; padding: 5px 10px; border-style: hidden;" | ||
placeholder="Type message and press Enter to send..."></textarea> | ||
</div> | ||
<div style="overflow: auto; border-style: ridge; border-top-style: hidden;"> | ||
<button class="btn-warning pull-right" id="echo">Echo</button> | ||
<button class="btn-success pull-right" id="sendmessage">Send</button> | ||
</div> | ||
<div class="modal alert alert-danger fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> | ||
<div class="modal-dialog" role="document"> | ||
<div class="modal-content"> | ||
<div class="modal-header"> | ||
<div>Connection Error...</div> | ||
<div><strong style="font-size: 1.5em;">Hit Refresh/F5</strong> to rejoin. ;)</div> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="modal alert alert-danger fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> | ||
<div class="modal-dialog" role="document"> | ||
<div class="modal-content"> | ||
<div class="modal-header"> | ||
<div>Connection Error...</div> | ||
<div><strong style="font-size: 1.5em;">Hit Refresh/F5</strong> to rejoin. ;)</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<!--Reference the SignalR library. --> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/6.0.1/signalr.js"></script> | ||
<!--Reference the SignalR library. --> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/6.0.1/signalr.js"></script> | ||
|
||
<!--Add script to update the page and send messages.--> | ||
<script type="text/javascript"> | ||
document.addEventListener('DOMContentLoaded', function () { | ||
function getUserName() { | ||
function generateRandomName() { | ||
return Math.random().toString(36).substring(2, 10); | ||
} | ||
<!--Add script to update the page and send messages.--> | ||
<script type="text/javascript"> | ||
document.addEventListener("DOMContentLoaded", function () { | ||
function getUserName() { | ||
function generateRandomName() { | ||
return Math.random().toString(36).substring(2, 10); | ||
} | ||
|
||
// Get the user name and store it to prepend to messages. | ||
var username = generateRandomName(); | ||
var promptMessage = 'Enter your name:'; | ||
do { | ||
username = prompt(promptMessage, username); | ||
if (!username || username.startsWith('_') || username.indexOf('<') > -1 || username.indexOf('>') > -1) { | ||
username = ''; | ||
promptMessage = 'Invalid input. Enter your name:'; | ||
} | ||
} while (!username) | ||
return username; | ||
} | ||
// Get the user name and store it to prepend to messages. | ||
var username = generateRandomName(); | ||
var promptMessage = "Enter your name:"; | ||
do { | ||
username = prompt(promptMessage, username); | ||
if (!username || username.startsWith("_") || username.indexOf("<") > -1 || username.indexOf(">") > -1) { | ||
username = ""; | ||
promptMessage = "Invalid input. Enter your name:"; | ||
} | ||
} while (!username) | ||
return username; | ||
} | ||
|
||
username = getUserName(); | ||
// Set initial focus to message input box. | ||
var messageInput = document.getElementById('message'); | ||
messageInput.focus(); | ||
username = getUserName(); | ||
// Set initial focus to message input box. | ||
var messageInput = document.getElementById("message"); | ||
messageInput.focus(); | ||
|
||
function createMessageEntry(encodedName, encodedMsg) { | ||
var entry = document.createElement('div'); | ||
entry.classList.add("message-entry"); | ||
if (encodedName === "_SYSTEM_") { | ||
entry.innerHTML = encodedMsg; | ||
entry.classList.add("text-center"); | ||
entry.classList.add("system-message"); | ||
} else if (encodedName === "_BROADCAST_") { | ||
entry.classList.add("text-center"); | ||
entry.innerHTML = `<div class="text-center broadcast-message">${encodedMsg}</div>`; | ||
} else if (encodedName === username) { | ||
entry.innerHTML = `<div class="message-avatar pull-right">${encodedName}</div>` + | ||
`<div class="message-content pull-right">${encodedMsg}<div>`; | ||
} else { | ||
entry.innerHTML = `<div class="message-avatar pull-left">${encodedName}</div>` + | ||
`<div class="message-content pull-left">${encodedMsg}<div>`; | ||
} | ||
return entry; | ||
} | ||
function createMessageEntry(encodedName, encodedMsg) { | ||
var entry = document.createElement("div"); | ||
entry.classList.add("message-entry"); | ||
if (encodedName === "_SYSTEM_") { | ||
entry.innerHTML = encodedMsg; | ||
entry.classList.add("text-center"); | ||
entry.classList.add("system-message"); | ||
} else if (encodedName === "_BROADCAST_") { | ||
entry.classList.add("text-center"); | ||
entry.innerHTML = `<div class="text-center broadcast-message">${encodedMsg}</div>`; | ||
} else if (encodedName === username) { | ||
entry.innerHTML = `<div class="message-avatar pull-right">${encodedName}</div>` + | ||
`<div class="message-content pull-right">${encodedMsg}<div>`; | ||
} else { | ||
entry.innerHTML = `<div class="message-avatar pull-left">${encodedName}</div>` + | ||
`<div class="message-content pull-left">${encodedMsg}<div>`; | ||
} | ||
return entry; | ||
} | ||
|
||
function appendMessage(encodedName, encodedMsg) { | ||
var messageEntry = createMessageEntry(encodedName, encodedMsg); | ||
var messageBox = document.getElementById("messages"); | ||
messageBox.appendChild(messageEntry); | ||
messageBox.scrollTop = messageBox.scrollHeight; | ||
} | ||
function appendMessage(encodedName, encodedMsg) { | ||
var messageEntry = createMessageEntry(encodedName, encodedMsg); | ||
var messageBox = document.getElementById("messages"); | ||
messageBox.appendChild(messageEntry); | ||
messageBox.scrollTop = messageBox.scrollHeight; | ||
} | ||
|
||
function bindConnectionMessage(connection) { | ||
var messageCallback = function (name, message) { | ||
if (!message) return; | ||
// Html encode display name and message. | ||
var encodedName = name; | ||
var encodedMsg = message.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">"); | ||
appendMessage(encodedName, encodedMsg); | ||
}; | ||
// Create a function that the hub can call to broadcast messages. | ||
connection.on('broadcastMessage', messageCallback); | ||
connection.on('echo', messageCallback); | ||
connection.onclose(onConnectionError); | ||
} | ||
function bindConnectionMessage(connection) { | ||
var messageCallback = function (name, message) { | ||
if (!message) return; | ||
// Html encode display name and message. | ||
var encodedName = name; | ||
var encodedMsg = message.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">"); | ||
appendMessage(encodedName, encodedMsg); | ||
}; | ||
// Create a function that the hub can call to broadcast messages. | ||
connection.on("broadcastMessage", messageCallback); | ||
connection.on("echo", messageCallback); | ||
connection.onclose(onConnectionError); | ||
} | ||
|
||
function onConnected(connection) { | ||
console.log('connection started'); | ||
connection.send('broadcastMessage', '_SYSTEM_', username + ' JOINED'); | ||
document.getElementById('sendmessage').addEventListener('click', function (event) { | ||
// Call the broadcastMessage method on the hub. | ||
if (messageInput.value) { | ||
connection.send('broadcastMessage', username, messageInput.value) | ||
.catch((e) => appendMessage("_BROADCAST_", e.message)); | ||
} | ||
function onConnected(connection) { | ||
console.log("connection started"); | ||
connection.send("broadcastMessage", "_SYSTEM_", username + " JOINED"); | ||
document.getElementById("sendmessage").addEventListener("click", function (event) { | ||
// Call the broadcastMessage method on the hub. | ||
if (messageInput.value) { | ||
connection.send("broadcastMessage", username, messageInput.value) | ||
.catch((e) => appendMessage("_BROADCAST_", e.message)); | ||
} | ||
|
||
// Clear text box and reset focus for next comment. | ||
messageInput.value = ''; | ||
messageInput.focus(); | ||
event.preventDefault(); | ||
}); | ||
document.getElementById('message').addEventListener('keypress', function (event) { | ||
if (event.keyCode === 13) { | ||
event.preventDefault(); | ||
document.getElementById('sendmessage').click(); | ||
return false; | ||
} | ||
}); | ||
document.getElementById('echo').addEventListener('click', function (event) { | ||
// Call the echo method on the hub. | ||
connection.send('echo', username, messageInput.value); | ||
// Clear text box and reset focus for next comment. | ||
messageInput.value = ""; | ||
messageInput.focus(); | ||
event.preventDefault(); | ||
}); | ||
document.getElementById("message").addEventListener("keypress", function (event) { | ||
if (event.keyCode === 13) { | ||
event.preventDefault(); | ||
document.getElementById("sendmessage").click(); | ||
return false; | ||
} | ||
}); | ||
document.getElementById("echo").addEventListener("click", function (event) { | ||
// Call the echo method on the hub. | ||
connection.send("echo", username, messageInput.value); | ||
|
||
// Clear text box and reset focus for next comment. | ||
messageInput.value = ''; | ||
messageInput.focus(); | ||
event.preventDefault(); | ||
}); | ||
} | ||
// Clear text box and reset focus for next comment. | ||
messageInput.value = ""; | ||
messageInput.focus(); | ||
event.preventDefault(); | ||
}); | ||
} | ||
|
||
function onConnectionError(error) { | ||
if (error && error.message) { | ||
console.error(error.message); | ||
} | ||
var modal = document.getElementById('myModal'); | ||
modal.classList.add('in'); | ||
modal.style = 'display: block;'; | ||
} | ||
function onConnectionError(error) { | ||
if (error && error.message) { | ||
console.error(error.message); | ||
} | ||
var modal = document.getElementById("myModal"); | ||
modal.classList.add("in"); | ||
modal.style = "display: block;"; | ||
} | ||
|
||
var connection = new signalR.HubConnectionBuilder() | ||
.withUrl('/chat') | ||
.build(); | ||
bindConnectionMessage(connection); | ||
connection.start() | ||
.then(function () { | ||
onConnected(connection); | ||
}) | ||
.catch(function (error) { | ||
console.error(error.message); | ||
}); | ||
var connection = new signalR.HubConnectionBuilder() | ||
.withUrl("/chat") | ||
.build(); | ||
bindConnectionMessage(connection); | ||
connection.start() | ||
.then(function () { | ||
onConnected(connection); | ||
}) | ||
.catch(function (error) { | ||
console.error(error.message); | ||
}); | ||
</script> | ||
}); | ||
</script> | ||
</body> | ||
</html> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<UserSecretsId>chatroom</UserSecretsId> | ||
<RootNamespace>Microsoft.Azure.SignalR.Samples.ChatRoom</RootNamespace> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="wwwroot\" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AspNet.Security.OAuth.GitHub" Version="7.0.2" /> | ||
<PackageReference Include="AspNet.Security.OAuth.GitHub" Version="7.0.4" /> | ||
<PackageReference Include="Microsoft.Azure.SignalR" Version="1.*" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.SignalR; | ||
|
||
[Authorize] | ||
public class ChatSampleHub : Hub | ||
{ | ||
public override Task OnConnectedAsync() | ||
{ | ||
return Clients.All.SendAsync("broadcastMessage", "_SYSTEM_", $"{Context.User?.Identity?.Name} JOINED"); | ||
} | ||
|
||
// Uncomment this line to only allow user in Microsoft to send message | ||
//[Authorize(Policy = "Microsoft_Only")] | ||
public Task BroadcastMessage(string message) | ||
{ | ||
return Clients.All.SendAsync("broadcastMessage", Context.User?.Identity?.Name, message); | ||
} | ||
|
||
public Task Echo(string message) | ||
{ | ||
var echoMessage = $"{message} (echo from server)"; | ||
return Clients.Client(Context.ConnectionId).SendAsync("echo", Context.User?.Identity?.Name, echoMessage); | ||
} | ||
} |
Oops, something went wrong.