以下是一个例子, http://www.venkatbaggu.com/signalr-database-update-notifications-asp-net-mvc-usiing-sql-dependency/
$(function () {
// Declare a proxy to reference the hub.
var notifications = $.connection.messagesHub;
//debugger;
// Create a function that the hub can call to broadcast messages.
notifications.client.updateMessages = function () {
getMyMessages();
};
// Start the connection.
$.connection.hub.start().done(function () {
//alert("connection started");
getMyMessages();
}).fail(function (e) {
alert(e);
});
});
function getMyMessages() {
var tbl = $('#mesTable');
$.ajax({
url: '/Home/GetMyMessages',
contentType: 'application/html ; charset:utf-8',
type: 'GET',
dataType: 'html'
}).success(function (result) {
tbl.empty().append(result);
}).error(function () {
});
}
我有上面的signalr代码。当我在本地计算机上运行它时它工作正常,但当我在IIS上部署并测试多个用户时,浏览器没有响应。
请告诉我这个错误的地方
集线器
[HubMethodName("sendMessages")]
public static void SendMyMessages()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MessagesHub>();
context.Clients.All.updateMessages();
}
----------------------------------------------------------------------
public IEnumerable<Messages> GetAllMessages()
{
var messages = new List<Messages>();
using (var connection = new SqlConnection(_connString))
{
connection.Open();
using (var command = new SqlCommand(@"SELECT [DoctorId], [DisplayName], [EmptyMessage], [Date] FROM [dbo].[Messages]", connection))
{
command.Notification = null;
var dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
var reader = command.ExecuteReader();
while (reader.Read())
{
messages.Add(item: new DocNotification { Id = (int)reader["Id"], DoctorId = (int)reader["DoctorId"], DisplayName = reader["DisplayName"] != DBNull.Value ? (string)reader["Message"] : "";
}
}
}
return messages;
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
MessagesHub.SendMyMessages();
}
}