Simple example of signalR not working

冷暖自知 提交于 2019-12-04 08:34:27
Ignacio Berra

Today I was working with the same issue.

First you need to add an atribute to your Hub with the name, as following:

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

The next to do is to change the order of your calls in the javascript. You need to do the connection next to instantiate the hub. So, the code will be as following:

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

I hope it works for you.

Please install the 1.0 version of SignalR from Nuget as well. From your script references it looks like you are using 0.5.2 and the latest supported version at the time of writing this post is 1.0. Please download Microsoft.AspNet.SignalR from Nuget

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!