A simple example does not work

It is not possible to get SignalR to work on my machine (with IE9). When you enter some text and press a button, the text does not appear in the list as intended. In addition, I expect the list to be updated from multiple instances of the browser, and this will not happen. There are no errors. Can anyone help here?

WITH#

namespace TestSignalR.Hubs
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    /// <summary>
    /// Summary description for ChatHub.
    /// </summary>
    public class ChatHub : SignalR.Hubs.Hub
    {
        public void TestMessage(string message)
        {
            Clients.writeMessage(message);
        }
    }
}

Aspx

<input type="text" name="txtInput" id="txtInput" />
<button id="btnSubmit">Submit</button>

<ul id="messages">
</ul>
<script type="text/javascript" src="SignalR/Hubs"></script>
<script type="text/javascript">
    $(document).ready(function (message) {
        var chat = $.connection.chatHub;

        chat.writeMessage = function (message) {
            $("#messages").append("<li>" + message + "</li>");
        };

        $("#btnSubmit").click(function () {
            var text = $("#txtInput").val();
            chat.testMessage(text);
        });

        $.connection.hub.start();
    });

</script>

On the main page there are links for jQuery and SignalR files: -

<script src="Scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.signalR-0.5.2.min.js" type="text/javascript"></script>
+5
source share
2 answers

Today I worked with the same problem.

First you need to add the attribute to your hub with the name, as follows:

[HubName("chathub")]
public class ChatHub : SignalR.Hubs.Hub

The next step is to reorder your calls in javascript. You need to make a connection next to the hub instance. Thus, the code will look like this:

$(document).ready(function (message) {
   var chat = $.connection.chatHub;

   $.connection.hub.start();

   chat.writeMessage = function (message) {
      $("#messages").append("<li>" + message + "</li>");
   };

    $("#btnSubmit").click(function () {
        var text = $("#txtInput").val();
        chat.testMessage(text);
    });        
 });

, .

+3

, 1.0 SignalR Nuget. script , 0.5.2, - 1.0. Microsoft.AspNet.SignalR Nuget

0

All Articles