Skip to content

chore(docs): add chat peer-to-peer demo telerik/kendo#19688 #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.AspNetCore.Mvc;

namespace Telerik.Examples.Mvc.Controllers.Chat
{
public class ChatPeerToPeerController : Controller
{
public IActionResult ChatPeerToPeer()
{
return View();
}
}
}
18 changes: 18 additions & 0 deletions Telerik.Examples.Mvc/Telerik.Examples.Mvc/Hubs/ChatHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;

namespace Telerik.Examples.Mvc.Hubs
{
public class ChatHub : Hub
{
public async Task Send(object sender, string message)
{
await Clients.Others.SendAsync("broadcastMessage", sender, message);
}

public async Task SendTyping(object sender)
{
await Clients.Others.SendAsync("typing", sender);
}
}
}
2 changes: 2 additions & 0 deletions Telerik.Examples.Mvc/Telerik.Examples.Mvc/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
{
options.ViewLocationFormats.Add("/Views/ListBox/{0}" + RazorViewEngine.ViewExtension);
options.ViewLocationFormats.Add("/Views/Captcha/{0}" + RazorViewEngine.ViewExtension);
options.ViewLocationFormats.Add("/Views/chat/{0}" + RazorViewEngine.ViewExtension);
options.ViewLocationFormats.Add("/Views/Grid/{0}" + RazorViewEngine.ViewExtension);
options.ViewLocationFormats.Add("/Views/ImageEditor/{0}" + RazorViewEngine.ViewExtension);
options.ViewLocationFormats.Add("/Views/Editor/{0}" + RazorViewEngine.ViewExtension);
Expand Down Expand Up @@ -191,6 +192,7 @@
endpoints.MapHub<GridHub>("/gridHub");
endpoints.MapHub<ParentGridHub>("/parentGridHub");
endpoints.MapHub<ChildGridHub>("/childGridHub");
endpoints.MapHub<ChatHub>("/chat");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<script src="~/js/signalr/dist/browser/signalr.min.js"></script>

@{
var name = Guid.NewGuid().ToString();
}

@(Html.Kendo().Chat()
.Name("chat")
.User(user => user
.Name(@name)
.IconUrl("https://demos.telerik.com/kendo-ui/content/chat/avatar.png")
)
.Events(events => events
.TypingStart("onTypingStart")
.Post("onPost")
)
)

<script>
window.chatHub = new signalR.HubConnectionBuilder()
.withUrl('/chat')
.build();

chatHub.start()
.catch(function(err) {
console.error(err.toString());
});

$(function() {
window.chat = $("#chat").getKendoChat();

chatHub.on('broadcastMessage', function(sender, message) {
var message = {
type: 'text',
text: message
};

chat.renderMessage(message, sender);
});

chatHub.on('typing', function(sender) {
chat.renderMessage({ type: 'typing' }, sender);
});
});

function onTypingStart(e) {
chatHub.invoke("sendTyping", chat.getUser());
}

function onPost(args) {
chatHub.invoke("send", chat.getUser(), args.text);
}
</script>