我在 56 分 11 秒部分的视频“使用 ASP.NET SignalR 扩展实时 Web ”中受到启发。
想象一个使用 SignalR 与服务器通信的基于 Web 的聊天客户端。当客户端连接时,其终结点信息存储在 Azure 表中。
聊天客户端可以通过 SignalR 向另一个聊天客户端发送消息,该客户端查找感兴趣的目标客户端的端点(可能在不同的实例上),然后使用 Web API 通过 SignalR 将消息发送到另一个实例到客户端。
为了演示,我已将示例应用程序上传到 github。
当只有一个 Azure 实例时,这一切都有效。但是,如果有 MULTIPLE azure 实例,则从服务器到客户端对 SignalR 的最终调用会静默失败。它就像动态代码不存在,或者它从一个“坏”线程中脱落,或者消息以某种方式发送到了错误的实例,或者我刚刚犯了一个愚蠢的错误。
任何想法将不胜感激。
网页是这样设置的
<input type="radio" name='ClientId' value='A' style='width:30px'/>Chat client A</br>
<input type="radio" name='ClientId' value='B' style='width:30px'/>Chat client B</br>
<input type='button' id='register' value='Register' />
<input type='text' id='txtMessage' size='50' /><input type='button' id='send' value='Send' />
<div id='history'>
</div>
JS是
<script type="text/javascript">
$(function () {
// Declare a proxy to reference the hub.
var chat = $.connection.chatHub;
chat.client.sendMessageToClient = function (message) {
$('#history').append("<br/>" + message);
};
// Start the connection.
$.connection.hub.start().done(function () {
$('#register').click(function () {
// Call the Send method on the hub.
chat.server.register($('input[name=ClientId]:checked', '#myForm').val());
});
$('#send').click(function () {
// Call the Send method on the hub.
chat.server.sendMessageToServer($('input[name=ClientId]:checked', '#myForm').val(), $('#txtMessage').val());
});
});
});
</script>
枢纽如下。(我有一个小存储类将端点信息存储在 Azure 表中)。注意静态方法 SendMessageToClient。这就是最终失败的原因。它是从 Web Api 类调用的(如下)
public class ChatHub : Hub
{
public void Register(string chatClientId)
{
Storage.RegisterChatEndPoint(chatClientId, this.Context.ConnectionId);
}
/// <summary>
/// Receives the message and sends it to the SignalR client.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="connectionId">The connection id.</param>
public static void SendMessageToClient(string message, string connectionId)
{
GlobalHost.ConnectionManager.GetHubContext<ChatHub>().Clients.Client(connectionId).SendMessageToClient(message);
Debug.WriteLine("Sending a message to the client on SignalR connection id: " + connectionId);
Debug.WriteLine("Via the Web Api end point: " + RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["WebApi"].IPEndpoint.ToString());
}
/// <summary>
/// Sends the message to other instance.
/// </summary>
/// <param name="chatClientId">The chat client id.</param>
/// <param name="message">The message.</param>
public void SendMessageToServer(string chatClientId, string message)
{
// Get the chatClientId of the destination.
string otherChatClient = (chatClientId == "A" ? "B" : "A");
// Find out this other chatClientId's end point
ChatClientEntity chatClientEntity = Storage.GetChatClientEndpoint(otherChatClient);
if (chatClientEntity != null)
ChatWebApiController.SendMessage(chatClientEntity.WebRoleEndPoint, chatClientEntity.SignalRConnectionId, message);
}
}
最后 CateWebApiController 是这个
public class ChatWebApiController : ApiController
{
[HttpGet]
public void SendMessage(string message, string connectionId)
{
//return message;
ChatHub.SendMessageToClient(message, connectionId);
}
/// <summary>
/// This calls the method above but on a different instance via Web API
/// </summary>
/// <param name="endPoint">The end point.</param>
/// <param name="connectionId">The connection id.</param>
/// <param name="message">The message.</param>
public static void SendMessage(string endPoint, string connectionId, string message)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://" + endPoint);
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string url = "http://" + endPoint + "/api/ChatWebApi/SendMessage/?Message=" + HttpUtility.UrlEncode(message) + "&ConnectionId=" + connectionId;
client.GetAsync(url);
}
}