I'm trying to find examples on how to get real time updates using a web service in ASP.NET MVC (Version doesn't matter) and posting it back to a specific user's browser window.
我正在寻找如何使用ASP中的web服务获得实时更新的示例。NET MVC(版本无关紧要)并将其发布到特定用户的浏览器窗口。
A perfect example would be a type of chat system like that of facebooks' where responses are send to the appropriate browser(client) whenever a message has been posted instead of creating a javascript timer on the page that checks for new messages every 5 seconds. I've heard tons of times about types of sync programs out there, but i'm looking for this in code, not using a third party software.
一个完美的例子是类似于facebooks的聊天系统,每当消息被发布时,响应就被发送到适当的浏览器(客户端),而不是在页面上创建一个javascript计时器,每5秒检查一次新消息。我已经听过很多次关于同步程序的类型,但是我在代码中寻找,而不是使用第三方软件。
What i'm looking to do specifically:
我特别想做的是:
I'm trying to create a web browser chat client that is SQL and Web Service based in ASP.NET MVC. When you have 2-4 different usernames logged into the system they chat and send messages to each other that is saved in an SQL database, then when there has been a new entry (or someone sent a new message) the Web Service see's this change and then shows the receiving user the new updated message. E.G Full Chat Synced Chat using a Web Service.
我正在尝试创建一个web浏览器聊天客户机,它是基于ASP的SQL和web服务。净MVC。有2 - 4不同的用户名登录到系统时他们互相聊天和发送消息,保存在一个SQL数据库,当有一个新条目(或某人发送新消息)Web服务看到这种变化,然后展示了新的更新消息接收用户。E。使用Web服务进行同步聊天。
The thing that really stomps me in general is I have no idea how to detect if something new is added to an SQL table, and also I have no idea how to send information from SQL to a specific user's web browser. So if there are people userA, userB, userC all on the website, i don't know how to only show a message to userC if they are all under the username "guest". I would love to know hot to do this feature not only for what i'm trying to create now, but for future projects as well.
我通常不知道如何检测SQL表中是否添加了新内容,也不知道如何从SQL发送信息到特定用户的web浏览器。如果网站上有用户userA userB userC,我不知道如何只显示一条消息给userC如果他们都在"guest"下。我很想知道hot做这个功能不仅是为了我现在正在尝试的,也是为了将来的项目。
Can anyone point me into the right direction please? I know SQL pretty well, and web services i'm intermediate with.
谁能告诉我正确的方向吗?我对SQL非常了解,对web服务也很了解。
1 个解决方案
#1
2
You can use SignalR for this task.
您可以使用SignalR执行此任务。
Via Scott Hanselman:
通过Scott Hanselman:
-
Create Asp.net mvc empty application
创建Asp.net mvc空白应用程序。
-
install nuget package of SignalR
安装SignalR的nuget包。
-
Add new Controller (as example HomeController):
添加新控制器(例如HomeController):
public class HomeController : Controller { public ActionResult Index() { return View(); } }
公共类HomeController:控制器{public ActionResult Index() {return View();} }
-
Create view Index with javascript references: @Url.Content("~/Scripts/jquery-1.6.4.min.js")" "@Url.Content("~/Scripts/jquery.signalR.js")" and function:
使用javascript引用创建视图索引:@Url.Content(“~/Scripts/jquery-1.6.4.min.js”)“.content”(“~ /脚本/ jquery.signalR.js”)和功能:
$(function () { var hub = $.connection.chatHub; hub.AddMessage = function (msg) { $('#messages').append('<li>' + msg + '</li>'); }; $.connection.hub.start().done(function() { $('#send').click(function() { hub.send($('#msg').val()); }); }); });
-
Create class ChatHub:
创建类ChatHub:
public class ChatHub:Hub { public void Send(string message) { Clients.AddMessage(message); } }
公共类ChatHub:Hub {public void Send(string message) {client . addmessage (message);} }
#1
2
You can use SignalR for this task.
您可以使用SignalR执行此任务。
Via Scott Hanselman:
通过Scott Hanselman:
-
Create Asp.net mvc empty application
创建Asp.net mvc空白应用程序。
-
install nuget package of SignalR
安装SignalR的nuget包。
-
Add new Controller (as example HomeController):
添加新控制器(例如HomeController):
public class HomeController : Controller { public ActionResult Index() { return View(); } }
公共类HomeController:控制器{public ActionResult Index() {return View();} }
-
Create view Index with javascript references: @Url.Content("~/Scripts/jquery-1.6.4.min.js")" "@Url.Content("~/Scripts/jquery.signalR.js")" and function:
使用javascript引用创建视图索引:@Url.Content(“~/Scripts/jquery-1.6.4.min.js”)“.content”(“~ /脚本/ jquery.signalR.js”)和功能:
$(function () { var hub = $.connection.chatHub; hub.AddMessage = function (msg) { $('#messages').append('<li>' + msg + '</li>'); }; $.connection.hub.start().done(function() { $('#send').click(function() { hub.send($('#msg').val()); }); }); });
-
Create class ChatHub:
创建类ChatHub:
public class ChatHub:Hub { public void Send(string message) { Clients.AddMessage(message); } }
公共类ChatHub:Hub {public void Send(string message) {client . addmessage (message);} }