private async void Go() { var hubConnection = new HubConnection("http://localhost:1235/"); var hub = hubConnection.CreateHubProxy("moveShape"); hub.On<double, double>("shapeMoved", (x, y) => Dispatcher.InvokeAsync(() => { Canvas.SetLeft(Shape, (Body.ActualWidth - Shape.ActualWidth) * x); Canvas.SetTop(Shape, (Body.ActualHeight - Shape.ActualHeight) * y); }) ); hub.On<int>("clientCountChanged", count => Dispatcher.InvokeAsync(() => ClientCount.Text = count.ToString())); await hubConnection.Start(); Shape.Draggable((left, top) => hub.Invoke("MoveShape", left / (Body.ActualWidth - Shape.ActualWidth), top / (Body.ActualHeight - Shape.ActualHeight)) ); Closing += (_, __) => hubConnection.Stop(); }
private static void RunDemoHub(HubConnection hubConnection) { var demo = hubConnection.CreateHubProxy("demo"); hubConnection.StateChanged += change => { Console.WriteLine(change.OldState + " => " + change.NewState); }; demo.On<int>("invoke", i => { int n = demo.GetValue<int>("index"); Console.WriteLine("{0} client state index -> {1}", i, n); }); hubConnection.Start().Wait(); demo.Invoke("multipleCalls").ContinueWith(task => { using (var error = task.Exception.GetError()) { Console.WriteLine(error); } }, TaskContinuationOptions.OnlyOnFaulted); Task.Factory.StartNew(() => { Thread.Sleep(7000); hubConnection.Stop(); }); }
static void Main(string[] args) { Console.Title = "SignalR Sample Windows Console Client"; HubConnection connection = new HubConnection("http://localhost:50991/"); IHubProxy hub = connection.CreateHubProxy("Echo"); connection.Start().ContinueWith(task => { if (task.IsFaulted) { Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException()); } else { Console.WriteLine("Success! Connected with client connection id {0}", connection.ConnectionId); // Do more stuff here } }); hub.Invoke<string>("Send", new { message = "Test" }).ContinueWith(task => { Console.WriteLine("Value from server {0}", task.Result); }); connection.Stop(); Console.ReadLine(); }
public void EndToEndTest(HostType hostType, TransportType transportType) { using (var host = CreateHost(hostType, transportType)) { host.Initialize(); var hubConnection = new HubConnection(host.Url); IHubProxy proxy = hubConnection.CreateHubProxy("ChatHub"); var wh = new ManualResetEvent(false); proxy.On("addMessage", data => { Assert.Equal("hello", data); wh.Set(); }); hubConnection.Start(host.Transport).Wait(); proxy.InvokeWithTimeout("Send", "hello"); Assert.True(wh.WaitOne(TimeSpan.FromSeconds(10))); hubConnection.Stop(); } }
static void Main(string[] args) { var server = new HubConnection("http://localhost:8001/"); var method = server.CreateHubProxy("kinect"); var sensor = Microsoft.Kinect.KinectSensor.KinectSensors.First(); try { server.Start().Wait(); sensor.SkeletonFrameReady += (sender, eventArgs) => { using (var frame = eventArgs.OpenSkeletonFrame()) { if (frame == null) return; var skeletons = new Skeleton[frame.SkeletonArrayLength]; frame.CopySkeletonDataTo(skeletons); var user = skeletons.FirstOrDefault(x => x.TrackingState == SkeletonTrackingState.Tracked); if (user == null) return; var m = user.Joints. Where(x => x.TrackingState == JointTrackingState.Tracked) .Select(s => { var c = sensor.CoordinateMapper.MapSkeletonPointToColorPoint(s.Position, ColorImageFormat.RgbResolution640x480Fps30); return new JointPosition { x = c.X, y = c.Y }; }).ToList(); Console.Write("."); method.Invoke("Send",m); } }; sensor.SkeletonStream.Enable(new TransformSmoothParameters() { Correction = 0.7f, JitterRadius = 0.1f, MaxDeviationRadius = 0.1f, Prediction = 0.7f, Smoothing = 0.8f }); sensor.Start(); Console.WriteLine("Started."); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.ReadLine(); } finally { sensor.Stop(); sensor.Dispose(); server.Stop(); } }
public static IDisposable BrodcastFromServer() { var host = new MemoryHost(); IHubContext context = null; host.Configure(app => { var config = new HubConfiguration() { Resolver = new DefaultDependencyResolver() }; app.MapHubs(config); var configuration = config.Resolver.Resolve<IConfigurationManager>(); // The below effectively sets the heartbeat interval to five seconds. configuration.KeepAlive = TimeSpan.FromSeconds(10); var connectionManager = config.Resolver.Resolve<IConnectionManager>(); context = connectionManager.GetHubContext("EchoHub"); }); var cancellationTokenSource = new CancellationTokenSource(); var thread = new Thread(() => { while (!cancellationTokenSource.IsCancellationRequested) { context.Clients.All.echo(); } }); thread.Start(); var connection = new Client.Hubs.HubConnection("http://foo"); var proxy = connection.CreateHubProxy("EchoHub"); try { connection.Start(host).Wait(); Thread.Sleep(1000); } finally { connection.Stop(); } return new DisposableAction(() => { cancellationTokenSource.Cancel(); thread.Join(); host.Dispose(); }); }
private static void RunOne(MemoryHost host) { var connection = new Client.Hubs.HubConnection("http://foo"); var proxy = connection.CreateHubProxy("OnConnectedOnDisconnectedHub"); try { connection.Start(host).Wait(); string guid = Guid.NewGuid().ToString(); string otherGuid = proxy.Invoke<string>("Echo", guid).Result; if (!guid.Equals(otherGuid)) { throw new InvalidOperationException("Fail!"); } } finally { connection.Stop(); } }
public static IDisposable StressGroups(int max = 100) { var host = new MemoryHost(); host.Configure(app => { var config = new HubConfiguration() { Resolver = new DefaultDependencyResolver() }; app.MapHubs(config); var configuration = config.Resolver.Resolve<IConfigurationManager>(); // The below effectively sets the heartbeat interval to five seconds. configuration.KeepAlive = TimeSpan.FromSeconds(10); }); var countDown = new CountDownRange<int>(Enumerable.Range(0, max)); var connection = new Client.Hubs.HubConnection("http://foo"); var proxy = connection.CreateHubProxy("HubWithGroups"); proxy.On<int>("Do", i => { if (!countDown.Mark(i)) { Debugger.Break(); } }); try { connection.Start(new Client.Transports.LongPollingTransport(host)).Wait(); proxy.Invoke("Join", "foo").Wait(); for (int i = 0; i < max; i++) { proxy.Invoke("Send", "foo", i).Wait(); } proxy.Invoke("Leave", "foo").Wait(); for (int i = max + 1; i < max + 50; i++) { proxy.Invoke("Send", "foo", i).Wait(); } if (!countDown.Wait(TimeSpan.FromSeconds(10))) { Console.WriteLine("Didn't receive " + max + " messages. Got " + (max - countDown.Count) + " missed " + String.Join(",", countDown.Left.Select(i => i.ToString()))); Debugger.Break(); } } finally { connection.Stop(); } return host; }
public static void SendLoop() { var host = new MemoryHost(); host.Configure(app => { var config = new HubConfiguration() { Resolver = new DefaultDependencyResolver() }; config.Resolver.Resolve<IConfigurationManager>().ConnectionTimeout = TimeSpan.FromDays(1); app.MapHubs(config); }); var connection = new Client.Hubs.HubConnection("http://foo"); var proxy = connection.CreateHubProxy("EchoHub"); var wh = new ManualResetEventSlim(false); proxy.On("echo", _ => wh.Set()); try { connection.Start(new Client.Transports.LongPollingTransport(host)).Wait(); while (true) { proxy.Invoke("Echo", "foo").Wait(); if (!wh.Wait(TimeSpan.FromSeconds(10))) { Debugger.Break(); } wh.Reset(); } } catch { connection.Stop(); } }
public static IDisposable RunConnectDisconnect(int connections) { var host = new MemoryHost(); host.Configure(app => { var config = new HubConfiguration() { Resolver = new DefaultDependencyResolver() }; app.MapHubs(config); }); for (int i = 0; i < connections; i++) { var connection = new Client.Hubs.HubConnection("http://foo"); var proxy = connection.CreateHubProxy("EchoHub"); var wh = new ManualResetEventSlim(false); proxy.On("echo", _ => wh.Set()); try { connection.Start(host).Wait(); proxy.Invoke("Echo", "foo").Wait(); if (!wh.Wait(TimeSpan.FromSeconds(10))) { Debugger.Break(); } } finally { connection.Stop(); } } return host; }
public void JoiningGroupMultipleTimesGetsMessageOnce(MessageBusType messagebusType) { using (var host = new MemoryHost()) { host.Configure(app => { var config = new HubConfiguration { Resolver = new DefaultDependencyResolver() }; UseMessageBus(messagebusType, config.Resolver); app.MapHubs(config); }); var connection = new HubConnection("http://foo"); var hub = connection.CreateHubProxy("SendToSome"); int invocations = 0; connection.Start(host).Wait(); hub.On("send", () => { invocations++; }); // Join the group multiple times hub.InvokeWithTimeout("JoinGroup", "a"); hub.InvokeWithTimeout("JoinGroup", "a"); hub.InvokeWithTimeout("JoinGroup", "a"); hub.InvokeWithTimeout("SendToGroup", "a"); Thread.Sleep(TimeSpan.FromSeconds(3)); Assert.Equal(1, invocations); connection.Stop(); } }
public static IDisposable RunConnectDisconnect(bool scaleout, int nodes = 1, int connections = 1000) { IHttpClient client; IDisposable disposable = TryGetClient(scaleout, nodes, out client); for (int i = 0; i < connections; i++) { var connection = new Client.Hubs.HubConnection("http://foo"); var proxy = connection.CreateHubProxy("EchoHub"); var wh = new ManualResetEventSlim(false); proxy.On("echo", _ => wh.Set()); try { connection.Start(client).Wait(); proxy.Invoke("Echo", "foo").Wait(); if (!wh.Wait(TimeSpan.FromSeconds(10))) { Debugger.Break(); } } finally { connection.Stop(); } } return disposable; }