2012-08-29 67 views
13

Tôi chỉ mới bắt đầu khám phá tín hiệu và tôi muốn có thể gửi tin nhắn từ máy chủ đến tất cả các máy khách.Làm cách nào để gửi tin nhắn từ máy chủ đến máy khách bằng SignalR Hubs

Đây là Hub tôi

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using SignalR; 
using SignalR.Hubs; 
using SignalR.Hosting.Common; 
using SignalR.Hosting.AspNet; 
using System.Threading.Tasks; 

namespace MvcApplication1 
{ 
    public class Chat : Hub 
    { 
     public void Send(String message) 
     { 
      // Call the addMessage methods on all clients 
      Clients.addMessage(message); 
     } 
    } 
} 

Đây là khách hàng của tôi Trang

 <script type="text/javascript"> 

     $(function() { 

      //Proxy created on the fly 
      var chat = $.connection.chat; 

      // Declare a function on the chat hub so the server can invoke it 
      chat.addMessage = function (message) { 
       $("#messages").append("<li>" + message + "</li>"); 
      }; 

      $("#broadcast").click(function() { 
       // call the chat method on the server 
       chat.send($("#msg").val()); 
      }); 

      $.connection.hub.start(); 
     }); 
    </script> 


} 



<input type="text" id="msg" /> 
     <input type="button" id="broadcast" value="broadcast" /> 

     <ul id="messages" class="round"> 


     </ul> 

này tất cả các công trình hoàn hảo, tôi có thể "trò chuyện" giữa 2 trình duyệt khác nhau.

Điều tiếp theo tôi muốn làm là khởi tạo thông báo từ máy chủ cho tất cả khách hàng.

Vì vậy, tôi đã thử điều này.

using SignalR; 
using System.Web.Http; 
using System.Web.Mvc; 
using System.Web.Optimization; 
using System.Web.Routing; 
using System; 
using System.Web.Routing; 
using SignalR; 
using SignalR.Hubs; 

namespace MvcApplication1 
{ 
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801 

    public class MvcApplication : System.Web.HttpApplication 
    { 
     protected void Application_Start() 
     {    
      var aTimer = new System.Timers.Timer(1000); 

      aTimer.Elapsed += aTimer_Elapsed; 
      aTimer.Interval = 3000; 
      aTimer.Enabled = true; 

      AreaRegistration.RegisterAllAreas(); 

      WebApiConfig.Register(GlobalConfiguration.Configuration); 
      FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
      RouteConfig.RegisterRoutes(RouteTable.Routes); 
      BundleConfig.RegisterBundles(BundleTable.Bundles); 
      AuthConfig.RegisterAuth(); 
     } 

     void aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 
     { 
      var context = GlobalHost.ConnectionManager.GetHubContext<Chat>(); 
      context.Clients.Send("Hello");  
     } 
    } 
} 

Điều này dường như không hoạt động. Bộ hẹn giờ hoạt động, Trình xử lý sự kiện "aTimer_Elapsed" chạy 3 giây một lần nhưng phương thức "Gửi" trên trung tâm trò chuyện không bao giờ chạy.

Bất kỳ ý tưởng nào?

Trả lời

25

Tôi nghĩ rằng nó phải được

void aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 
    { 
     var context = GlobalHost.ConnectionManager.GetHubContext<Chat>(); 
     context.Clients.All.addMessage("Hello");  
    } 

để thay thế. Với Gửi bạn đang gọi phương thức được sử dụng bởi khách hàng để gọi máy chủ ...

+1

điều gì xảy ra nếu tôi muốn gửi tin nhắn cho một khách hàng cụ thể, trong trường hợp dữ liệu trong bảng trong db bị thay đổi, trực tiếp sử dụng DBMS Console hoặc từ ứng dụng dành cho máy tính để bàn hoặc trang web của tôi? –

+0

@MuhammadMamoorKhan Để giải quyết một khách hàng cá nhân, bạn cần phải biết ID kết nối của khách hàng. Sau đó, bạn có thể thực hiện 'GlobalHost.ConnectionManager.GetHubContext () .Clients.Client (connectionId) .addMessage (" something here ");' – Corey

0

Có bạn phải thiết lập đường đó để:

void aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 
    { 
     var context = GlobalHost.ConnectionManager.GetHubContext<Chat>(); 
     context.Clients.All.addMessage("Hello");  
    } 

Tuy nhiên đây chỉ là một nửa và vẫn wont work.

Trong Js của bạn, bạn cần phải viết:

$(function() { 

//Proxy created on the fly 
var chat = $.connection.chat; 

// Declare a function on the chat hub so the server can invoke it 
chat.client.addMessage = function (message) { 
    $("#messages").append("<li>" + message + "</li>"); 
}; 

$("#broadcast").click(function() { 
    // call the chat method on the server 
    chat.client.addMessage($("#msg").val()); 
}); 

$.connection.hub.start(); 
}); 

tôi thêm chat.client này sẽ thêm một phương pháp trung tâm client-side rằng máy chủ sẽ gọi.

Các vấn đề liên quan