简体   繁体   中英

Hub not able to send a message to console client

I have implemented SignalR for my Windows Azure project. I have two clients - Javascript/HTML client in my web role and a console application in my project. And Web role is my SignalR server. When i put the web role and the console application as the start up projects, the messages i send from the HTML client are sent to the console application. But when i put the Cloud project and the console application as the start up projects, the messages from the HTML client are not being sent to the console application. Its really weird, i dont know what could be the difference between the two which is causing the problem.

And if i put a background thread in my web role which will send messages to connected clients periodically, it works on both occasions, i mean the console app and the HTML client are receiving messages irrespective of the start up projects.

Please let me know if you have any idea what the problem is

My Hub:

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

My HTML/Javascript client:

<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>

My Console app client:

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);
    }
}

Background Thread:

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));
            }
        });
    }
}

I tried this scenario with my application and I was able to send messages from a webrole to a console application. Is it possible to zip your project and send it to see if this reproes...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM