Skip to content

Commit f857a74

Browse files
ivaylo-milanovVicTachev
authored andcommitted
chore(docs): add chat peer-to-peer demo telerik/kendo#19688
1 parent af7ab6a commit f857a74

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
namespace Telerik.Examples.Mvc.Controllers.Chat
4+
{
5+
public class ChatPeerToPeerController : Controller
6+
{
7+
public IActionResult ChatPeerToPeer()
8+
{
9+
return View();
10+
}
11+
}
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Microsoft.AspNetCore.SignalR;
2+
using System.Threading.Tasks;
3+
4+
namespace Telerik.Examples.Mvc.Hubs
5+
{
6+
public class ChatHub : Hub
7+
{
8+
public async Task Send(object sender, string message)
9+
{
10+
await Clients.Others.SendAsync("broadcastMessage", sender, message);
11+
}
12+
13+
public async Task SendTyping(object sender)
14+
{
15+
await Clients.Others.SendAsync("typing", sender);
16+
}
17+
}
18+
}

Telerik.Examples.Mvc/Telerik.Examples.Mvc/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
{
6464
options.ViewLocationFormats.Add("/Views/ListBox/{0}" + RazorViewEngine.ViewExtension);
6565
options.ViewLocationFormats.Add("/Views/Captcha/{0}" + RazorViewEngine.ViewExtension);
66+
options.ViewLocationFormats.Add("/Views/chat/{0}" + RazorViewEngine.ViewExtension);
6667
options.ViewLocationFormats.Add("/Views/Grid/{0}" + RazorViewEngine.ViewExtension);
6768
options.ViewLocationFormats.Add("/Views/ImageEditor/{0}" + RazorViewEngine.ViewExtension);
6869
options.ViewLocationFormats.Add("/Views/Editor/{0}" + RazorViewEngine.ViewExtension);
@@ -191,6 +192,7 @@
191192
endpoints.MapHub<GridHub>("/gridHub");
192193
endpoints.MapHub<ParentGridHub>("/parentGridHub");
193194
endpoints.MapHub<ChildGridHub>("/childGridHub");
195+
endpoints.MapHub<ChatHub>("/chat");
194196
endpoints.MapControllerRoute(
195197
name: "default",
196198
pattern: "{controller=Home}/{action=Index}/{id?}");
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<script src="~/js/signalr/dist/browser/signalr.min.js"></script>
2+
3+
@{
4+
var name = Guid.NewGuid().ToString();
5+
}
6+
7+
@(Html.Kendo().Chat()
8+
.Name("chat")
9+
.User(user => user
10+
.Name(@name)
11+
.IconUrl("https://demos.telerik.com/kendo-ui/content/chat/avatar.png")
12+
)
13+
.Events(events => events
14+
.TypingStart("onTypingStart")
15+
.Post("onPost")
16+
)
17+
)
18+
19+
<script>
20+
window.chatHub = new signalR.HubConnectionBuilder()
21+
.withUrl('/chat')
22+
.build();
23+
24+
chatHub.start()
25+
.catch(function(err) {
26+
console.error(err.toString());
27+
});
28+
29+
$(function() {
30+
window.chat = $("#chat").getKendoChat();
31+
32+
chatHub.on('broadcastMessage', function(sender, message) {
33+
var message = {
34+
type: 'text',
35+
text: message
36+
};
37+
38+
chat.renderMessage(message, sender);
39+
});
40+
41+
chatHub.on('typing', function(sender) {
42+
chat.renderMessage({ type: 'typing' }, sender);
43+
});
44+
});
45+
46+
function onTypingStart(e) {
47+
chatHub.invoke("sendTyping", chat.getUser());
48+
}
49+
50+
function onPost(args) {
51+
chatHub.invoke("send", chat.getUser(), args.text);
52+
}
53+
</script>

0 commit comments

Comments
 (0)