protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { var host = new WebSocketHost(serviceType, baseAddresses); var binding = WebSocketHost.CreateWebSocketBinding(https: false, subProtocol: "chatprotocol"); host.AddWebSocketEndpoint(binding); return host; }
private static void Main() { var sh = new WebSocketHost<EchoService>(new Uri("ws://" + Environment.MachineName + ":4502/echo")); sh.AddWebSocketEndpoint(); sh.Open(); Console.WriteLine("Websocket echo server listening on " + sh.Description.Endpoints[0].Address.Uri.AbsoluteUri); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("NOTE:"); Console.WriteLine("1. Validate http://localhost/clientaccesspolicy.xml is accessible from the browser before running the echo client."); Console.WriteLine(" (See detailed instructions in clientaccesspolicy.xml in the EchoService project.)"); Console.WriteLine("2. Ensure the firewall allows incoming TCP traffic on port 4502 before running the echo client."); Console.ResetColor(); Console.WriteLine(); Console.WriteLine("Press Ctrl-C to terminate the echo server..."); using (ManualResetEvent manualResetEvent = new ManualResetEvent(false)) { manualResetEvent.WaitOne(); } sh.Close(); }
public async Task Test1() { var epsilon = TimeSpan.FromSeconds(0.5); await using var serving = await WebSocketHost.ServeAsync(); var client = Services.GetRequiredService <IClientTimeService>(); var cTime = await Computed.CaptureAsync(_ => client.GetTimeAsync()); cTime.Options.AutoInvalidateTime.Should().Be(ComputedOptions.Default.AutoInvalidateTime); if (!cTime.IsConsistent()) { cTime = await cTime.UpdateAsync(false); cTime.IsConsistent().Should().BeTrue(); } (DateTime.Now - cTime.Value).Should().BeLessThan(epsilon); await TestEx.WhenMet( () => cTime.IsConsistent().Should().BeFalse(), TimeSpan.FromSeconds(5)); var time = await cTime.UseAsync(); (DateTime.Now - time).Should().BeLessThan(epsilon); }
public async Task TestFormattedTime() { await using var serving = await WebSocketHost.ServeAsync(); var service = Services.GetRequiredService <IClientTimeService>(); (await service.GetFormattedTimeAsync("")).Should().Be(""); (await service.GetFormattedTimeAsync("null")).Should().Be(""); var format = "{0}"; var matchCount = 0; for (int i = 0; i < 20; i++) { var time = await service.GetTimeAsync(); var formatted = await service.GetFormattedTimeAsync(format); var expected = string.Format(format, time); if (formatted == expected) { matchCount++; } await Task.Delay(TimeSpan.FromSeconds(0.1)); } matchCount.Should().BeGreaterThan(2); }
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { var host = new WebSocketHost(serviceType, baseAddresses); host.AddWebSocketEndpoint(); return host; }
public async Task TestRewriteClient() { await using var serving = await WebSocketHost.ServeAsync(); var tfv = Services.GetTypeViewFactory <IEdgeCaseService>(); var client = Services.GetRequiredService <IEdgeCaseRewriteClient>(); var service = tfv.CreateView(client); // ReSharper disable once SuspiciousTypeConversion.Global (service is TypeView <IEdgeCaseService>).Should().BeTrue(); // ReSharper disable once SuspiciousTypeConversion.Global (service is TypeView <IEdgeCaseRewriteClient, IEdgeCaseService>).Should().BeTrue(); // This part tests that proxy builder generates // a different type for each combination of <TView, TImplementation> var otherClient = Services.GetRequiredService <IEdgeCaseClient>(); var otherService = tfv.CreateView(otherClient); service.GetType().Should().NotBeSameAs(otherService.GetType()); ThrowIfContainsErrorExceptionType = typeof(ServiceException); ThrowIfContainsErrorNonComputeExceptionType = typeof(ServiceException); ThrowIfContainsErrorRewriteErrorsExceptionType = typeof(ServiceException); await ActualTest(service); }
public async Task TimerTest() { await using var serving = await WebSocketHost.ServeAsync(); var tp = Services.GetRequiredService <ITimeService>(); var pub = await Publisher.PublishAsync(_ => tp.GetTimeAsync()); var rep = Replicator.GetOrAdd <DateTime>(pub.Ref); await rep.RequestUpdateAsync().AsAsyncFunc() .Should().CompleteWithinAsync(TimeSpan.FromMinutes(1)); var count = 0; using var state = StateFactory.NewLive <DateTime>( o => o.WithZeroUpdateDelay(), async(_, ct) => await rep.Computed.UseAsync(ct)); state.Updated += s => { Out.WriteLine($"Client: {s.Value}"); count++; }; await TestEx.WhenMet( () => count.Should().BeGreaterThan(2), TimeSpan.FromSeconds(5)); }
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { WebSocketHost host = new WebSocketHost(serviceType, baseAddresses); host.AddWebSocketEndpoint(); return(host); }
static void Main(string[] args) { var baseAddress = new Uri("ws://localhost:20001/EchoService2"); using (var host = new WebSocketHost <EchoService2>(baseAddress)) { host.AddWebSocketEndpoint(); host.Open(); Console.WriteLine(baseAddress.ToString() + " Opened ..."); var task = new Task(() => { while (true) { System.Threading.Thread.Sleep(10000); try { Console.WriteLine("Service Instance Count:" + EchoService2.ActivityServices.Count); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } } }); task.Start(); Console.Read(); } }
static void Main(string[] args) { DiffContainer.Init(100); var host = new WebSocketHost(typeof(Connection), new ServiceThrottlingBehavior() { MaxConcurrentSessions = int.MaxValue, MaxConcurrentCalls = int.MaxValue, MaxConcurrentInstances = int.MaxValue }, new Uri("ws://localhost:9080/PolyJoin")); var binding = WebSocketHost.CreateWebSocketBinding(https: false, sendBufferSize: int.MaxValue, receiveBufferSize: int.MaxValue); binding.SendTimeout = TimeSpan.FromMilliseconds(5000); binding.OpenTimeout = TimeSpan.FromDays(1); host.AddWebSocketEndpoint(binding); Console.WriteLine("Open host"); host.Open(); host.Faulted += (sender, eventArgs) => { Console.WriteLine("Host falted"); }; CommonService.Init(); Console.ReadLine(); Console.WriteLine("Close host"); host.Close(); }
public async Task CommandTest() { // Server commands var kv = Services.GetRequiredService <IKeyValueService <string> >(); (await kv.GetAsync("")).Should().BeNull(); await kv.SetCommandAsync(new IKeyValueService <string> .SetCommand("", "1")); (await kv.GetAsync("")).Should().Be("1"); await Services.Commander().CallAsync(new IKeyValueService <string> .SetCommand("", "2")); (await kv.GetAsync("")).Should().Be("2"); // Client commands await using var serving = await WebSocketHost.ServeAsync(); var kvc = ClientServices.GetRequiredService <IKeyValueServiceClient <string> >(); await kvc.SetCommandAsync(new IKeyValueService <string> .SetCommand("", "1")); await Task.Delay(100); // Remote invalidation takes some time (await kvc.GetAsync("")).Should().Be("1"); await ClientServices.Commander().CallAsync(new IKeyValueService <string> .SetCommand("", "2")); await Task.Delay(100); // Remote invalidation takes some time (await kvc.GetAsync("")).Should().Be("2"); }
/// <summary> /// Initializes the gameService. /// </summary> private static void Main() { string appName = ConfigurationManager.AppSettings["AppName"]; string portNumber = ConfigurationManager.AppSettings["PortNumber"]; string machineName = ConfigurationManager.AppSettings["MachineName"]; string uriString = machineName + ":" + portNumber + "/" + appName; var sh = new WebSocketHost<GameService>(new Uri("ws://" + uriString)); sh.AddWebSocketEndpoint(); sh.Open(); Console.WriteLine("Websocket game server listening on " + sh.Description.Endpoints[0].Address.Uri.AbsoluteUri); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("NOTE:"); Console.WriteLine("1. Validate http://localhost/clientaccesspolicy.xml is accessible from the browser before running the game client."); Console.WriteLine(" (See detailed instructions in clientaccesspolicy.xml in the GameService project.)"); Console.WriteLine("2. Ensure the firewall allows incoming TCP traffic on port 4502 before running the game client."); Console.ResetColor(); Console.WriteLine(); Console.WriteLine("Press Ctrl-C to terminate the game server..."); using (ManualResetEvent manualResetEvent = new ManualResetEvent(false)) { manualResetEvent.WaitOne(); } sh.Close(); }
public async Task BasicTest() { await using var serving = await WebSocketHost.ServeAsync(); var sp = Services.GetRequiredService <ISimplestProvider>(); sp.SetValue(""); var p1 = await Publisher.PublishAsync(_ => sp.GetValueAsync()); p1.Should().NotBeNull(); var r1 = Replicator.GetOrAdd <string>(p1.Ref, true); var r1c = await r1.Computed.UpdateAsync(false); r1c.IsConsistent.Should().BeTrue(); r1c.Value.Should().Be(""); r1.Computed.Should().Be(r1c); sp.SetValue("1"); await Task.Delay(100); r1c.IsConsistent.Should().BeFalse(); r1.Computed.Should().Be(r1c); r1c = await r1c.UpdateAsync(false); r1c.Value.Should().Be("1"); var r1c1 = await r1c.UpdateAsync(false); r1c1.Should().Be(r1c); }
private async Task OnProcess(ResponseBodyBuilder response, CancellationToken token) { using (var socket = new WebSocketHost(response.GetStream())) using (var watch = new ServerSessionWatch()) { socket.Send("open"); var wRef = new WeakReference <WebSocketHost>(socket); watch.SessionChanged += (o, e) => { if (wRef.TryGetTarget(out var _socket)) { try { _socket.Send("message", e.Data); } catch (Exception error) { Log.Warn("Failed to send WebSocket message!", error); } } }; watch.Initialize(); // TODO: GET RID OF THIS!!! // This is an awful hack, and never stops running! // Need to read request and close both when closed. while (!token.IsCancellationRequested) { await Task.Delay(200, token); } } } }
public async Task ConnectToPublisherTest() { await using var serving = await WebSocketHost.ServeAsync(); var channel = await ConnectToPublisherAsync(); channel.Writer.Complete(); }
public async Task Test1() { var epsilon = TimeSpan.FromSeconds(0.5); await using var serving = await WebSocketHost.ServeAsync(); var client = ClientServices.GetRequiredService <IClientTimeService>(); var cTime = await Computed.CaptureAsync(_ => client.GetTimeAsync(default));
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { var host = new WebSocketHost(serviceType, baseAddresses); var binding = WebSocketHost.CreateWebSocketBinding(https: false, subProtocol: "chatprotocol"); host.AddWebSocketEndpoint(binding); return(host); }
public async Task ServerTimeModelTest1() { await using var serving = await WebSocketHost.ServeAsync(); using var stm = Services.GetRequiredService <ILiveState <ServerTimeModel> >(); var c = stm.State; c.IsConsistent.Should().BeFalse(); c.Value.Time.Should().Be(default);
public async Task TestClient() { await using var serving = await WebSocketHost.ServeAsync(); var client = Services.GetRequiredService <IEdgeCaseClient>(); var tfv = Services.GetTypeViewFactory <IEdgeCaseService>(); var service = tfv.CreateView(client); await ActualTest(service); }
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { WebSocketHost host = new WebSocketHost(serviceType, baseAddresses); var bindingSsl = WebSocketHost.CreateWebSocketBinding(true); host.AddWebSocketEndpoint(bindingSsl); //host.AddWebSocketEndpoint(); return(host); }
public async Task NoConnectionTest() { await using var serving = await WebSocketHost.ServeAsync(); var tp = Services.GetRequiredService <ITimeService>(); var pub = await Publisher.PublishAsync(_ => tp.GetTimeAsync()); var rep = Replicator.GetOrAdd <DateTime>(("NoPublisher", pub.Id)); await rep.RequestUpdateAsync().AsAsyncFunc() .Should().ThrowAsync <WebSocketException>(); }
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { var host = new WebSocketHost(serviceType, new ServiceThrottlingBehavior { MaxConcurrentSessions = int.MaxValue, MaxConcurrentCalls = 20 }, baseAddresses); var binding = WebSocketHost.CreateWebSocketBinding(https: false, sendBufferSize: 2048, receiveBufferSize: 2048); binding.SendTimeout = TimeSpan.FromMilliseconds(500); binding.OpenTimeout = TimeSpan.FromDays(1); host.AddWebSocketEndpoint(binding); return host; }
/// <summary> /// Demonstrate how to create the WebSocket in WCF. /// </summary> /// <param name="args"></param> static void Main(string[] args) { var host = new WebSocketHost <MyWebSocketService>(new Uri("ws://localhost:8080/daenetsocket")); var endpoint = host.AddWebSocketEndpoint(); host.Open(); Console.WriteLine("Socket has been initialized. Press any key to exit."); Console.ReadLine(); host.Close(); }
internal static void Start(string uri) { //localBind = WebSocketHost.CreateWebSocketBinding(false, 1024, 1024); localBind = WebSocketHost.CreateWebSocketBinding(false); serverLog = new WebSocketHost <WsUploadService>(new Uri(uri)); serverLog.AddWebSocketEndpoint(localBind); serverLog.Credentials.UseIdentityConfiguration = true; serverLog.Faulted += serverChat_Faulted; serverLog.Opened += serverChat_Opened; serverLog.Open(); }
public async Task BasicTest() { var kv = Services.GetRequiredService <IKeyValueService <string> >(); (await kv.TryGetAsync("")).Should().Be(Option.None <string>()); (await kv.GetAsync("")).Should().BeNull(); await kv.SetAsync("", "1"); (await kv.TryGetAsync("")).Should().Be(Option.Some("1")); (await kv.GetAsync("")).Should().Be("1"); await using var serving = await WebSocketHost.ServeAsync(); using var kvm = Services.GetRequiredService <ILiveState <KeyValueModel <string> > >(); var kvc = Services.GetRequiredService <IKeyValueServiceClient <string> >(); // First read var c = kvm.Computed; c.IsConsistent().Should().BeFalse(); c.Value.Key.Should().Be(""); c.Value.Value.Should().BeNull(); c.Value.UpdateCount.Should().Be(0); await TestEx.WhenMetAsync(() => { var snapshot = kvm.Snapshot; snapshot.Computed.HasValue.Should().BeTrue(); var c = snapshot.Computed; c.IsConsistent().Should().BeTrue(); c.Value.Key.Should().Be(""); c.Value.Value.Should().Be("1"); c.Value.UpdateCount.Should().Be(1); }, TimeSpan.FromSeconds(1)); // Update await kvc.SetAsync(kvm.Computed.Value.Key, "2"); await Task.Delay(300); c = kvm.Computed; c.IsConsistent().Should().BeFalse(); c.Value.Value.Should().Be("1"); c.Value.UpdateCount.Should().Be(1); await Task.Delay(1000); c = kvm.Computed; c.IsConsistent().Should().BeTrue(); c.Value.Value.Should().Be("2"); c.Value.UpdateCount.Should().Be(2); }
public async Task ExceptionTest() { var kv = Services.GetRequiredService <IKeyValueService <string> >(); await using var serving = await WebSocketHost.ServeAsync(); var kvc = Services.GetRequiredService <IKeyValueServiceClient <string> >(); try { await kvc.GetAsync("error"); } catch (ApplicationException ae) { ae.Message.Should().Be("Error!"); } }
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { var host = new WebSocketHost(serviceType, new ServiceThrottlingBehavior { MaxConcurrentSessions = int.MaxValue, MaxConcurrentCalls = 20 }, baseAddresses); var binding = WebSocketHost.CreateWebSocketBinding(https: false, sendBufferSize: 2048, receiveBufferSize: 2048); binding.SendTimeout = TimeSpan.FromMilliseconds(500); binding.OpenTimeout = TimeSpan.FromDays(1); host.AddWebSocketEndpoint(binding); return(host); }
public async Task ClientTimeServiceTest() { await using var serving = await WebSocketHost.ServeAsync(); var service = Services.GetRequiredService <IClientTimeService>(); var time = await service.GetTimeAsync(); (DateTime.Now - time).Should().BeLessThan(TimeSpan.FromSeconds(1)); await Task.Delay(TimeSpan.FromSeconds(2)); time = await service.GetTimeAsync(); (DateTime.Now - time).Should().BeLessThan(TimeSpan.FromSeconds(1)); }
public async Task BasicTest() { var kv = Services.GetRequiredService <IKeyValueService <string> >(); (await kv.GetValueAsync("")).Should().Be(Option.None <string>()); await kv.SetValueAsync("", "1"); (await kv.GetValueAsync("")).Should().Be(Option.Some("1")); await using var serving = await WebSocketHost.ServeAsync(); using var kvm = Services.GetRequiredService <ILiveState <KeyValueModel <string> > >(); var kvc = Services.GetRequiredService <IStringKeyValueClient>(); // First read var c = kvm.State; c.IsConsistent.Should().BeFalse(); c.Value.Key.Should().Be(""); c.Value.Value.Should().BeNull(); c.Value.UpdateCount.Should().Be(0); await Task.Delay(300); kvm.UpdateError.Should().BeNull(); c = kvm.State; c.IsConsistent.Should().BeTrue(); c.Value.Key.Should().Be(""); c.Value.Value.Should().Be("1"); c.Value.UpdateCount.Should().Be(1); // Update await kvc.SetValueAsync(kvm.State.Value.Key, "2"); await Task.Delay(300); c = kvm.State; c.IsConsistent.Should().BeFalse(); c.Value.Value.Should().Be("1"); c.Value.UpdateCount.Should().Be(1); await Task.Delay(1000); c = kvm.State; c.IsConsistent.Should().BeTrue(); c.Value.Value.Should().Be("2"); c.Value.UpdateCount.Should().Be(2); }
public async Task Test2() { var epsilon = TimeSpan.FromSeconds(0.5); await using var serving = await WebSocketHost.ServeAsync(); var service = Services.GetRequiredService <IClientTimeService>(); for (int i = 0; i < 20; i++) { var time = await service.GetTimeAsync(); (DateTime.Now - time).Should().BeLessThan(epsilon); await Task.Delay(TimeSpan.FromSeconds(0.1)); } }
public async Task TimeServiceClientTest() { await using var serving = await WebSocketHost.ServeAsync(); var client = Services.GetRequiredService <ITimeServiceClient>(); var cTime = await client.GetComputedTimeAsync(); cTime.IsConsistent.Should().BeTrue(); (DateTime.Now - cTime.Value).Should().BeLessThan(TimeSpan.FromSeconds(1)); await Task.Delay(TimeSpan.FromSeconds(2)); cTime.IsConsistent.Should().BeFalse(); var time = await cTime.UseAsync(); (DateTime.Now - time).Should().BeLessThan(TimeSpan.FromSeconds(1)); }
public LogSocketsServer(string uri) { //localBind = WebSocketHost.CreateWebSocketBinding(false, 1024, 1024); localBind = WebSocketHost.CreateWebSocketBinding(false); serverLog = new WebSocketHost <LogOutputService>(new Uri(uri)); serverLog.AddWebSocketEndpoint(localBind); serverLog.Credentials.UseIdentityConfiguration = true; serverLog.Faulted += (se, ev) => { }; serverLog.Opened += (se, ev) => { Type type = se.GetType(); ; }; serverLog.Open(); }
public async Task BasicTest() { var epsilon = TimeSpan.FromSeconds(0.5); await using var serving = await WebSocketHost.ServeAsync(); var service = Services.GetRequiredService <IScreenshotServiceClient>(); ScreenshotController.CallCount = 0; for (int i = 0; i < 20; i++) { var screenshot = await service.GetScreenshotAsync(100); (DateTime.Now - screenshot.CapturedAt).Should().BeLessThan(epsilon); await Task.Delay(TimeSpan.FromSeconds(0.1)); } ScreenshotController.CallCount.Should().Be(1); }
public async Task Test1() { var epsilon = TimeSpan.FromSeconds(0.5); await using var serving = await WebSocketHost.ServeAsync(); var client = Services.GetRequiredService <IClientTimeService>(); var cTime = await Computed.CaptureAsync(_ => client.GetTimeAsync()); cTime.IsConsistent.Should().BeTrue(); (DateTime.Now - cTime.Value).Should().BeLessThan(epsilon); await Task.Delay(TimeSpan.FromSeconds(2)); cTime.IsConsistent.Should().BeFalse(); var time = await cTime.UseAsync(); (DateTime.Now - time).Should().BeLessThan(epsilon); }
public async Task TimerTest() { await using var serving = await WebSocketHost.ServeAsync(); var tp = Services.GetRequiredService <ITimeService>(); var pub = await Publisher.PublishAsync(_ => tp.GetTimeAsync()); var rep = Replicator.GetOrAdd <DateTime>(pub.Ref); var count = 0; using var _ = rep.Computed.AutoUpdate((c, o, _) => { Out.WriteLine($"{c.Value}"); count++; }); await Task.Delay(1100); count.Should().BeGreaterThan(2); }