How do I send token information to the server side with signalR?

89 Views Asked by At
  var ajaxResponse = $.ajaxQueue({
    type: "POST",
    url: "/Upload/Upload",
    enctype: 'multipart/form-data',
    contentType: false,
    processData: false,
    async: true,
    beforeSend: function(request) {
        request.setRequestHeader("serviceUrl", getToLocalStorage("serviceUrl"));
        request.setRequestHeader("token", getToLocalStorage("token"));
        connection.on("lastprogress", function (itemId, progresbar) {
            
            $(".progress-bar").each(function (index, element) {
                var fileid = $(this).data("fileid");
                if (fileid === itemId) {
                    $(this).width(progresbar + "%");
                }
                var obj = CancelUploadFileList.find(el => el == object.itemId);
                if (obj) {
                    $(".progress-bar").each(function (index, element) {
                        var fileid = $(this).data("fileid"); 
                        if (fileid === object.itemId) {
                         
                            $(this).removeClass("progress-bar");
                            $(this).parent().removeClass("progress-custom");
                            $(this).html("<div style='font-weight:normal;'>Canceled</div>");
                            ajaxResponse.abort();
                           // $(this).innerHTML("Canceled");
                        }
                    });
                    
                    
                }
            });
         //   var progressBarValue = $(".progress-custom").find("[data-fileid='" + uploadFileCollectionList[i].itemId + "']").val();
            
         //   $('.progress-bar').eq(count - 1).width(progresbar + "%");
            //if (progresbar == 100) {
            //    connection.on("uploadcontrol", function (isboolean, itemId) {
            //        if (isboolean) {
            //            $('.remove-from-list').each(function (index, element) {

            //                if ($(this).data("fileid") === itemId) {
            //                    $(this).removeClass("red").removeClass("stop").addClass("green").addClass("check");
            //                }
            //            });


            //        }

            //    });
            //}
        }
        );
        try {
            connection.start().catch(err => console.log(err.toString())).then(response => console.log("connected"));//unutma
        } catch (e) {

        }
         
    },
    headers: { 'serviceUrl': getToLocalStorage("serviceUrl"), 'token': getToLocalStorage("token") },
    data: formData,
      success: function (data) {
          if (data.success) {
              UploadControl(newItemId, object.overWrite);
          }
          else {
              $(function () {
                  new PNotify({
                      //
                      text: data.message == null ? object.itemName+" file is empty": data.message,
                      type: "error",
                      addClass: ".notifybg",
                      delay: 5000


                  });
              });
          }
     

        
      },
      error: function (data) {
          var uploadedFile = uploadFileCollectionListData.findIndex(el => el.itemId == newItemId);
          uploadFileCollectionListData.splice(uploadedFile, 1); 
      }

});

 while (bytesToRead > 0)
                {
                    int n = stream.Read(buffer, 0, chunkSize);

                    if (n == 0) break;

                    if (n != buffer.Length)
                        Array.Resize(ref buffer, n);


                    var newToken= _progressHub.Clients.Client().In
                    uploadCloudItem = UploadCloudItem(token, new MemoryStream(buffer), u);
                    bytesRead += n;
                    bytesToRead -= n;
                    var progress = (int)((float)bytesRead / (float)uploadRequest.itemSize * 100.0);
                   
                    _progressHub.Clients.All.SendAsync("lastprogress", u.itemId, progress);
                    Task.Delay(500);
                }

How do I send token information to the server side with signalR? How do I send token information to the server side with signalR? I'm uploading files with jquery, but the token expires when the file size is large. how do i send my current token How do I send token information to the server side with signalR?

1

There are 1 best solutions below

0
tuvikhoahocnet On

If a website needs to display in real time or so graph the up and down graphs of stocks. They will think of programming so that the browser regularly sends requests to get the latest stock prices from the web server -> Pooling mechanism. If the number of browsers simultaneously accessing thousands, the web server will have to work very hard even if only a few stocks change points, but still have to reply and send back all the stocks. . Instead of letting the client ask the server (pooling) continuously blindly. Why not let the server, if there are changes, will report back to the client, and will only send the necessary data for the client to process (notification)