集线器无法向控制台客户端发送消息

时间:2013-01-31 07:31:47

标签: c# asp.net azure signalr signalr-hub

我已经为我的Windows Azure项目实现了SignalR。我有两个客户端 - 我的Web角色中的Javascript / HTML客户端和我项目中的控制台应用程序。 Web角色是我的SignalR服务器。当我将Web角色和控制台应用程序作为启动项目时,我从HTML客户端发送的消息将发送到控制台应用程序。但是当我将Cloud项目和控制台应用程序作为启动项目时,来自HTML客户端的消息不会被发送到控制台应用程序。它真的很奇怪,我不知道造成问题的两者之间可能存在什么区别。

如果我在我的web角色中放置一个后台线程,它会定期向连接的客户端发送消息,它可以在两种情况下工作,我的意思是控制台应用程序和HTML客户端正在接收消息而不管启动项目。

如果你知道问题是什么,请告诉我

我的中心:

public class BroadcastHub : Hub
{
    public void Send(PersistedAudioRecord record)
    {
        // Call the BroadcastAudio method to update clients.
        Clients.All.BroadcastAudio(record);
    }
}

我的HTML / Javascript客户端:

<script type="text/javascript">
    $(function () {

        // Declare a proxy to reference the hub. 
        var broadcast = $.connection.broadcastHub;
        // Create a function that the hub can call to broadcast messages.
        broadcast.client.broadcastAudio = function (record) {
            // Html encode user name, channel and  title. 
            var encodedName = $('<div />').text(record.Username).html();
            var encodedChannel = $('<div />').text(record.Channel).html();
            var encodedTitle = $('<div />').text(record.Title).html();
            // Add the broadcast to the page. 
            $('#broadcasts').append('<li><strong>' + encodedName
                + '</strong>:&nbsp;&nbsp;' + encodedChannel + '</strong>:&nbsp;&nbsp;' + encodedTitle + '</li>');
        };
        // Get the user name.
        $('#displayname').val(prompt('Enter your name:', ''));
        // Get the Channel name to which you want to broadcast.
        $('#channelname').val(prompt('Enter Channel:', ''));
        // Set initial focus to message input box.  
        $('#title').focus();
        // Start the connection.
        $.connection.hub.start().done(function () {
            $('#sendbroadcast').click(function () {
                // Call the Send method on the hub. 
                var broadcastMessage = {}
                broadcastMessage.Username = $('#displayname').val();
                broadcastMessage.Channel = $('#channelname').val();
                broadcastMessage.Title = $('#title').val();
                broadcast.server.send(broadcastMessage);
                // Clear text box and reset focus for next broadcast. 
                $('#title').val('').focus();
            });
        });
    });
</script>

我的控制台应用客户端:

class Program
{
    static void Main(string[] args)
    {
        HubConnection connection = new HubConnection("http://localhost:35540/");
        IHubProxy proxy = connection.CreateHubProxy("BroadcastHub");
        proxy.On<AudioRecord>("BroadcastAudio", BroadcastAudio);

        connection.Start().Wait();

        Console.ReadLine();
    }

    static void BroadcastAudio(AudioRecord record)
    {
        Console.WriteLine("Broadcast: {0} {1} {2}", record.Username, record.Channel, record.Title);
    }
}

后台主题:

public class BackgroundThread
{
    private static Random _random = new Random();

    public static void Start()
    {
        ThreadPool.QueueUserWorkItem(_ =>
        {
            IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<BroadcastHub>();

            while (true)
            {
                PersistedAudioRecord record = new PersistedAudioRecord();                    
                record.Channel = _random.Next(10).ToString();
                record.Username = new string('a', Convert.ToInt32(record.Channel));
                record.Title = new string('b', Convert.ToInt32(record.Channel));

                try
                {
                    hubContext.Clients.All.BroadcastAudio(record);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Trace.TraceError("SignalR error thrown: {0}", ex);
                }
                Thread.Sleep(TimeSpan.FromSeconds(2));
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

我用我的应用程序尝试了这个场景,我能够将消息从webrole发送到控制台应用程序。是否可以压缩您的项目并发送它以查看是否有这种责任......