Server
public class ServerHub : Hub { public async Task<string> ALongTimeTask() { var p = new Progress<int>(); p.ProgressChanged += (a, count) => { Clients.Caller.progressChanged(count); }; var ret = await DoLongTimeTask(p); return ret; } private async Task<string> DoLongTimeTask(IProgress<int> p) { for (int i = 0; i < 100; i++) { p.Report(i); await Task.Delay(100); } return "finish"; } }
c#调用:
var hubConnection = new HubConnection("http://abc.com/signalr"); client = hubConnection.CreateHubProxy("ServerHub"); client.On("progressChanged", (count) => { //display current count; }); var t = await client.Invoke<string>("ALongTimeTask"); MessageBox.Show(t);
javascript调用
var chat = $.connection.serverHub; chat.client.progressChanged = function (count) { // display count }; chat.server.aLongTimeTask().done(function (result) { // display result });