2013-04-23 8 views
15

मैं सिग्नलआर विकी प्रारंभिक हब्स पेज से नमूना चैट एप्लिकेशन का उपयोग कर रहा हूं। मैंने इसे समूह समर्थन जोड़ने के लिए बढ़ा दिया है और यह ठीक काम कर रहा है।सिग्नलआर नेट क्लाइंट: मैं समूह को संदेश कैसे भेजूं?

हालांकि, अब मैं बाहरी कंसोल एप्लिकेशन से समूह को एक संदेश भेजना चाहता हूं। कंसोल ऐप के लिए मेरा कोड और नीचे समूह के लिए मेरा कोड है। मैं प्रॉक्सी से समूह को संदेश कैसे भेजूं? क्या यह संभव है?

// Console App 
using System; 
using Microsoft.AspNet.SignalR.Client.Hubs; 

namespace SignalrNetClient 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // Connect to the service 
      var connection = new HubConnection("http://localhost:50116"); 
      var chatHub = connection.CreateHubProxy("Chat"); 

      // Print the message when it comes in 
      connection.Received += data => Console.WriteLine(data); 

      // Start the connection 
      connection.Start().Wait(); 

      chatHub.Invoke("Send", "Hey there!"); 

      string line = null; 
      while ((line = Console.ReadLine()) != null) 
      { 
       // Send a message to the server 
       connection.Send(line).Wait(); 
      } 
     } 
    } 
} 

SignalR वेब एप्लिकेशन होस्ट:

namespace SignalrServer.Hubs 
{ 
    public class Chat : Hub 
    { 
     public void Send(string message) 
     { 
      // Call the addMessage method on all clients    
      Clients.All.addMessage(message); 
      Clients.Group("RoomA").addMessage("Group Message " + message); 
     } 

     //server 
     public void Join(string groupName) 
     { 
      Groups.Add(Context.ConnectionId, groupName); 
     } 
    } 
} 

Default.aspx

<script src="http://code.jquery.com/jquery-1.8.2.min.js" type="text/javascript"></script> 
<script src="Scripts/jquery.signalR-1.0.1.min.js" type="text/javascript"></script> 
<!-- If this is an MVC project then use the following --> 
<!-- <script src="~/signalr/hubs" type="text/javascript"></script> --> 
<script src="signalr/hubs" type="text/javascript"></script> 
<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.client.addMessage = function (message) { 
      $('#messages').append('<li>' + message + '</li>'); 
     }; 

     $.connection.hub.start(function() { 
      chat.server.join("RoomA"); 
     }); 

     // Start the connection 
     $.connection.hub.start().done(function() { 

      $("#broadcast").click(function() { 
       // Call the chat method on the server 
       chat.server.send($('#msg').val()); 
      }); 
     }); 
    }); 
</script> 

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

<ul id="messages"> 
</ul> 
    </div> 
+0

मैं इसे ले आप नाम पता संदेश भेजने से पहले समूह का? –

+0

हां, मेरे पास समूह का नाम है। – robrtc

+0

अभी के लिए मैं समूह के नाम को संदेश के साथ जोड़ता हूं और फिर भेजें विधि के अंदर स्ट्रिंग को विभाजित करता हूं। हालांकि, अगर मैं इसके लिए एक और अधिक सुरुचिपूर्ण समाधान है तो मैं उत्सुक हूं। – robrtc

उत्तर

24

क्या मैं कुछ इसी तरह एक तरीका है जिसके अपनी पसंद करता है, उदा की एक वस्तु को स्वीकार करता है के साथ किया है

आपका नया वर्ग

public class MyMessage{ 
    public string Msg { get; set; } 
    public string Group { get; set; } 
} 

फिर हब कि इस वस्तु को स्वीकार करता है में एक विधि पैदा करते हैं।

public void Send(MyMessage message) 
{ 
    // Call the addMessage method on all clients    
    Clients.All.addMessage(message.Msg); 
    Clients.Group(message.Group).addMessage("Group Message " + message.Msg); 
} 
फिर अपने ग्राहक से

, तो आप में इस वस्तु पारित कर सकते हैं।

chatHub.Invoke<MyMessage>("send", new MyMessage() { Msg = "Hello World", Group = "RoomA" }); 

फिर आप भी इस जे एस ग्राहक से कॉल कर सकते हैं

chat.server.send({ Msg: "Hello World", Group: "RoomA" }); 
+0

बहुत अच्छा समाधान। धन्यवाद! – robrtc

+0

कोई जांच नहीं। मुझे लगता है कि कोड सही है। यह मेरे सिर के ऊपर से किया गया था, तो इसे जाने दो। –

+0

कोड – robrtc

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