没有将SingalR消息发送到客户端,没有任何错误

时间:2019-07-25 07:14:28

标签: c# signalr

我有一个枢纽课程:

 public class OrderHub : Hub
    {
        public override Task OnConnected()
        {
            Clients.Caller.orderMessage("New connection");
            return base.OnConnected();
        }

        public static void SendOrderMessage(string msg)
        {
            var hubContext = GlobalHost.ConnectionManager.GetHubContext<OrderHub>();
            hubContext.Clients.All.orderMessage(msg);
        }
    }

正在调用静态方法的事件处理程序:

    public async Task Handle(IOrderProcessedEvent message, IMessageHandlerContext context)
    {
        // Here we could write a code with SignalR to notify the page about successful process

        while (true)
        {
            await Task.Delay(1000);

            //orderHub.OrderMessage("Order processed successfully");
            OrderHub.SendOrderMessage("Order processed successfully");
        }
    }

还有应该记录消息的简单JavaScript代码(这是一个非常简化的示例):

@section scripts{
    <script src="~/Scripts/jquery-1.9.1.min.js"></script>
    <script src="~/Scripts/jquery.signalR-2.4.1.min.js"></script>
    <script src="/signalr/hubs"></script>
    <script language="javascript">
        $(function () {
            var hub = $.connection.orderHub;
            console.log("hub initiated");

            hub.orderMessage = function (msg) {
                console.log(msg);


            };

            $.connection.hub.start();
        });
    </script>

缺少一些东西,因为从未调用过该函数,不是从OnConnected调用,而不是从静态方法调用。

console.log("hub initiated");行被打印,并且hub变量有效,但是实际的function(msg)从未被调用。

我想念什么?

1 个答案:

答案 0 :(得分:1)

您缺少“客户”:

hub.client.orderMessage = function (msg) {
            console.log(msg);


        };