私は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>
JSクライアントからこれを呼び出すことができますメッセージを送信する前にグループの –
はい、私はグループ名を持っています。 – robrtc
今のところ、グループ名とメッセージを結合し、Sendメソッド内で文字列を分割します。しかし、もっと洗練されたソリューションがあれば、私は不思議です。 – robrtc