2012-08-29 35 views
13

私はsignalRを探索し始めています。サーバーからすべてのクライアントにメッセージを送信したいと考えています。SignalRハブを使用してサーバーからクライアントにメッセージを送信するには

はここでここでこのすべての作品は完全に、私は2つの異なるブラウザ間で「チャット」することができる午前私のクライアントのページ

 <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> 

である私のハブ

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); 
     } 
    } 
} 

です。

私がやりたいことは、サーバーからすべてのクライアントにメッセージを送信することです。

だから私はこれを試しました。

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");  
     } 
    } 
} 

これは機能していないようです。タイマーが機能し、 "aTimer_Elapsed"イベントハンドラーが3秒ごとに実行されますが、チャットハブの "送信"メソッドは決して実行されません。

アイデア?

答えて

25

私はそれが代わりに

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

されるべきだと思います。センドを使用すると、サーバーを呼び出すために、クライアントによって使用されるメソッドを呼び出している...

+1

DB内のテーブルのデータが変更された場合、DBMSコンソールを使用して直接、またはデスクトップアプリケーションまたはWebサイトからメッセージを送信する場合はどうすればよいですか? –

+0

@MuhammadMamoorKhan個々のクライアントに対処するには、クライアントの接続IDを知る必要があります。次に、 'GlobalHost.ConnectionManager.GetHubContext ().Clients.Client(connectionId).addMessage(" something here ");' – Corey

0

はい、あなたはにそのラインを設定する必要があります。

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

しかし、これは半分だけの方法ですと、まだ仕事を文句を言いません。

あなたが書く必要があり、あなたのJsで

$(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(); 
}); 

私は、これは、サーバーが呼び出すクライアント側ハブメソッドを追加しますchat.clientを追加しました。

関連する問題