2013-04-23 7 views
15

私はSignalR Wiki Getting Started HubsページのサンプルChatアプリケーションを使用しています。私はグループのサポートを追加するためにそれを拡張し、それは正常に動作しています。SignalR .Netクライアント:どのようにしてグループにメッセージを送信できますか?

ただし、外部コンソールアプリケーションからグループにメッセージを送信したいとします。ここではコンソールアプリケーション用のコードを、その下にはグループ用のコードを示します。プロキシからグループにメッセージを送信するにはどうすればよいですか?出来ますか?

// 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 Webアプリケーションのホスト:

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

JSクライアントからこれを呼び出すことができますメッセージを送信する前にグループの –

+0

はい、私はグループ名を持っています。 – robrtc

+0

今のところ、グループ名とメッセージを結合し、Sendメソッド内で文字列を分割します。しかし、もっと洗練されたソリューションがあれば、私は不思議です。 – 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

probsはありません。私はコードが正しいと思います。それは私の頭の上から行われたので、それを行ってください。 –

+0

コードが動作しています – robrtc

関連する問題