SignalR客户端停止接收消息

时间:2015-05-08 15:43:54

标签: c# asp.net-mvc signalr

我的方案是,我有一个表单,允许用户上传文件。 上传此文件后,将对其进行处理,通常需要约40秒。因此,我想使用SignalR将操作进度报告给用户。我通过添加connectionId作为隐藏元素来执行此操作,以便将其提交给我的控制器的action方法。在我的动作方法中,我然后连接一些事件处理程序并通过获取hubcontext并发送消息来更新进度。

然而问题是,在第一次更新后,客户端再也没有收到任何消息了。我已经验证过,事件处理程序会被调用。

我的表格代码:

 $(".selectForm").submit(function (event) {
      var progress = $.connection.progressHub;

     // client-side sendMessage function that will be called from the server-side
                    progress.client.sendMessage = function (message) {
                        UpdateProgress(message);
                    };

     // establish the connection to the server
                    $.connection.hub.start().done(function () {
                        var connectionId = $.connection.hub.id;

    //create hidden element for the connectionId, so that it is submitted with the form
                        var input = $("<input>")
                                .attr("type", "hidden")
                                .attr("name", "connectionId")
                                .attr("class", "connectionId")
                                .val(connectionId);
                        $form.append($(input));
                        $form.submit();
                    });



           function UpdateProgress(message) {
                     //display the progress
                            $("#currentAction")
                                .html(message);
                        }
    }

在服务器上:

          [HttpPost]
            public ActionResult Save(string connectionId, MyModel model)
            {
                if (ModelState.IsValid)
                {

                    ...

                    var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>(); 
                    hubContext.Clients.Client(connectionId).sendMessage("File has been received");
                    var processing = new Processing(connectionId);

                    processing.Step1 += OnStep1;
                    processing.Step2 += OnStep2;
                    processing.Step3 += OnStep3;

                    processing.Start();
                }
            }


           private void OnStep1(object sender, EventArgs e)
            {
                var identArgs = e as IdentArgs;
                if (identArgs != null)
                {
                    //this message is received by the client
                    var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>();
                    hubContext.Clients.Client(identArgs.ConnectionId).sendMessage("Step1");
                }
            }


           private void OnStep2(object sender, EventArgs e)
            {
                var identArgs = e as IdentArgs;
                if (identArgs != null)
                {
                    //but this message is not
                    var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>();
                    hubContext.Clients.Client(identArgs.ConnectionId).sendMessage("Step2");
                }
            }

打开服务器和客户端上的跟踪会产生以下输出:

[17:20:36 GMT+0200 (W. Europe Daylight Time)] SignalR: Client subscribed to hub 'progresshub'.
jquery.signalR-2.0.2.min.js:24 [17:20:36 GMT+0200 (W. Europe Daylight Time)] SignalR: Negotiating with '/signalr/negotiate?connectionData=%5B%7B%22name%22%3A%22progresshub%22%7D%5D&clientProtocol=1.3'.
jquery.signalR-2.0.2.min.js:24 [17:20:36 GMT+0200 (W. Europe Daylight Time)] SignalR: Attempting to connect to SSE endpoint 'http://localhost:58610/signalr/connect?transport=serverSentEvents&connectio…au5Eet9Z9p&connectionData=%5B%7B%22name%22%3A%22progresshub%22%7D%5D&tid=6'.
jquery.signalR-2.0.2.min.js:24 [17:20:36 GMT+0200 (W. Europe Daylight Time)] SignalR: EventSource connected.
jquery.signalR-2.0.2.min.js:24 [17:20:36 GMT+0200 (W. Europe Daylight Time)] SignalR: Now monitoring keep alive with a warning timeout of 5333.333333333333 and a connection lost timeout of 8000.
jquery.signalR-2.0.2.min.js:24 [17:20:36 GMT+0200 (W. Europe Daylight Time)] SignalR: Invoking progresshub.SubscribeForProgress
jquery.signalR-2.0.2.min.js:24 [17:20:36 GMT+0200 (W. Europe Daylight Time)] SignalR: Invoked progresshub.SubscribeForProgress
jquery.signalR-2.0.2.min.js:24 [17:20:44 GMT+0200 (W. Europe Daylight Time)] SignalR: Triggering client hub event 'sendMessage' on hub 'ProgressHub'.
jquery.signalR-2.0.2.min.js:24 [17:20:47 GMT+0200 (W. Europe Daylight Time)] SignalR: Triggering client hub event 'sendMessage' on hub 'ProgressHub'.
jquery.signalR-2.0.2.min.js:24 [17:20:49 GMT+0200 (W. Europe Daylight Time)] SignalR: EventSource readyState: 0.
jquery.signalR-2.0.2.min.js:24 [17:20:49 GMT+0200 (W. Europe Daylight Time)] SignalR: EventSource reconnecting due to the server connection ending.
jquery.signalR-2.0.2.min.js:24 [17:20:51 GMT+0200 (W. Europe Daylight Time)] SignalR: EventSource calling close().
jquery.signalR-2.0.2.min.js:24 [17:20:51 GMT+0200 (W. Europe Daylight Time)] SignalR: serverSentEvents reconnecting.
jquery.signalR-2.0.2.min.js:24 [17:20:51 GMT+0200 (W. Europe Daylight Time)] SignalR: Attempting to connect to SSE endpoint 'http://localhost:58610/signalr/reconnect?transport=serverSentEvents&connect…C1%7CE%2C0&connectionData=%5B%7B%22name%22%3A%22progresshub%22%7D%5D&tid=4'.
jquery.signalR-2.0.2.min.js:24 [17:20:56 GMT+0200 (W. Europe Daylight Time)] SignalR: EventSource calling close().
jquery.signalR-2.0.2.min.js:24 [17:20:56 GMT+0200 (W. Europe Daylight Time)] SignalR: serverSentEvents reconnecting.
jquery.signalR-2.0.2.min.js:24 [17:20:56 GMT+0200 (W. Europe Daylight Time)] SignalR: Attempting to connect to SSE endpoint 'http://localhost:58610/signalr/reconnect?transport=serverSentEvents&connect…C1%7CE%2C0&connectionData=%5B%7B%22name%22%3A%22progresshub%22%7D%5D&tid=6'.
jquery.signalR-2.0.2.min.js:24 [17:21:01 GMT+0200 (W. Europe Daylight Time)] SignalR: EventSource calling close().
jquery.signalR-2.0.2.min.js:24 [17:21:01 GMT+0200 (W. Europe Daylight Time)] SignalR: serverSentEvents reconnecting.
jquery.signalR-2.0.2.min.js:24 [17:21:01 GMT+0200 (W. Europe Daylight Time)] SignalR: Attempting to connect to SSE endpoint 'http://localhost:58610/signalr/reconnect?transport=serverSentEvents&connect…C1%7CE%2C0&connectionData=%5B%7B%22name%22%3A%22progresshub%22%7D%5D&tid=9'.
jquery.signalR-2.0.2.min.js:24 [17:21:06 GMT+0200 (W. Europe Daylight Time)] SignalR: EventSource calling close().
jquery.signalR-2.0.2.min.js:24 [17:21:06 GMT+0200 (W. Europe Daylight Time)] SignalR: serverSentEvents reconnecting.
jquery.signalR-2.0.2.min.js:24 [17:21:06 GMT+0200 (W. Europe Daylight Time)] SignalR: Attempting to connect to SSE endpoint 'http://localhost:58610/signalr/reconnect?transport=serverSentEvents&connect…C1%7CE%2C0&connectionData=%5B%7B%22name%22%3A%22progresshub%22%7D%5D&tid=1'.
jquery.signalR-2.0.2.min.js:24 [17:21:11 GMT+0200 (W. Europe Daylight Time)] SignalR: EventSource calling close().
jquery.signalR-2.0.2.min.js:24 [17:21:11 GMT+0200 (W. Europe Daylight Time)] SignalR: serverSentEvents reconnecting.
jquery.signalR-2.0.2.min.js:24 [17:21:11 GMT+0200 (W. Europe Daylight Time)] SignalR: Attempting to connect to SSE endpoint 'http://localhost:58610/signalr/reconnect?transport=serverSentEvents&connect…1%7CE%2C0&connectionData=%5B%7B%22name%22%3A%22progresshub%22%7D%5D&tid=10'.

SignalR.Transports.TransportHeartBeat Information: 0 : Connection 9395a49c-85c7-4a57-a194-b8d9e55a89c8 is New.
SignalR.Transports.TransportHeartBeat Verbose: 0 : KeepAlive(9395a49c-85c7-4a57-a194-b8d9e55a89c8)
SignalR.Transports.TransportHeartBeat Verbose: 0 : KeepAlive(9395a49c-85c7-4a57-a194-b8d9e55a89c8)
SignalR.Transports.TransportHeartBeat Information: 0 : Dispose(). Closing all connections
SignalR.Transports.ServerSentEventsTransport Information: 0 : End(9395a49c-85c7-4a57-a194-b8d9e55a89c8)
SignalR.Transports.ServerSentEventsTransport Verbose: 0 : Cancel(9395a49c-85c7-4a57-a194-b8d9e55a89c8)
SignalR.Transports.ServerSentEventsTransport Verbose: 0 : DrainWrites(9395a49c-85c7-4a57-a194-b8d9e55a89c8)
SignalR.Transports.ServerSentEventsTransport Information: 0 : CompleteRequest (9395a49c-85c7-4a57-a194-b8d9e55a89c8)

我已经读过了 thisthis已经,但没有任何突出的东西可以解释这一点。

我的问题是:

  1. 我对SignalR和Web开发很新。有什么东西我显然做错了/次优?
  2. 我还能做些什么来缩小根本原因?
  3. 这样做有更简单的方法吗?
  4. 如果能提供任何帮助,我将不胜感激。

0 个答案:

没有答案