2012-08-29 7 views
13

का उपयोग कर सर्वर से क्लाइंट से संदेश कैसे भेजूं मैं सिग्नलआर का पता लगाने शुरू कर रहा हूं और मैं सर्वर से सभी ग्राहकों को संदेश भेजना चाहता हूं।मैं सिग्नलआर हब्स

यहाँ मेरी हब

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

है यहाँ मेरे मुवक्किल पृष्ठ

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

है यह सब काम करता है पूरी तरह से, मैं 2 अलग ब्राउज़रों के बीच "चैट" कर रहा हूँ।

अगली चीज़ जो मैं करना चाहता हूं वह सर्वर से सभी ग्राहकों को एक संदेश शुरू करना है।

तो मैंने कोशिश की।

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

यह काम नहीं प्रतीत होता है। टाइमर काम करता है, "एटिमर_इप्लेस्ड" इवेंट हैंडलर हर 3 सेकंड चलाता है लेकिन चैट हब पर "भेजें" विधि कभी नहीं चलती है।

कोई विचार?

उत्तर

25

मुझे लगता है कि यह

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

होना चाहिए। संदेश के साथ आप ग्राहक के द्वारा उपयोग सर्वर कॉल करने के लिए विधि बुला रहे हैं ...

+1

क्या होगा यदि मैं किसी विशिष्ट क्लाइंट को एक संदेश भेजने में सक्षम होना चाहता हूं, तो डीबी में किसी तालिका में डेटा बदल दिया गया है, या तो सीधे डीबीएमएस कंसोल या डेस्कटॉप ऐप या मेरी वेबसाइट से? –

+0

@MuhammadMamoorKhan एक व्यक्तिगत ग्राहक को संबोधित करने के लिए आपको ग्राहक की कनेक्शन आईडी जानने की आवश्यकता है। फिर आप 'GlobalHost.ConnectionManager.GetHubContext () कर सकते हैं। क्लाइंट्स। क्लाइंट (कनेक्शन आईडी) .addMessage ("यहां कुछ"); ' – Corey

0

हाँ आप के लिए है कि रेखा सेट करना होगा:

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

हालांकि यह केवल आधा रास्ता है और अभी भी अभ्यस्त काम है।

अपने जे एस में आप लिखने की ज़रूरत:

$(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 इस जोड़ा एक क्लाइंट-साइड हब विधि है कि सर्वर कॉल करेंगे जोड़ देगा।

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^