static void Main(string[] args) { Task.Run(async () => { var conHub = new HubConnection("http://localhost:8080/"); conHub.CreateHubProxy("Shell").On<ShellCommandParams>("cmd", (data) => { }); using (var con = new Connection("http://localhost:8080/api/cmd")) { con.Received += (data) => { Console.WriteLine($"ola, recebi! {data}"); }; con.StateChanged += (state) => { if (state.NewState != state.OldState) { Console.WriteLine($"De {state.OldState} para {state.NewState}"); } }; await con.Start(); await con.Send("Hello Mello"); } }).Wait(); }
private async Task RunPersistentConnection(string url, CookieContainer cookieContainer) { _traceWriter.WriteLine(); _traceWriter.WriteLine("*** Persistent Connection ***"); using (var connection = new Connection(url + "echo")) { connection.CookieContainer = cookieContainer; connection.TraceWriter = _traceWriter; connection.Received += data => { connection.TraceWriter.WriteLine("Received: " + data); }; connection.Error += exception => { connection.TraceWriter.WriteLine("Error: {0}: {1}" + exception.GetType(), exception.Message); }; await connection.Start(); await connection.Send("sending to AuthorizeEchoConnection"); await Task.Delay(TimeSpan.FromSeconds(2)); } }
public async Task SendAsync(string data) { if (_conn == null) { throw new Exception("Connection is not started."); } await _conn.Send(data); }
public async Task RunAsync(string url) { _connection = new Connection(url); _connection.TraceWriter = _traceWriter; _connection.Received += (data) => { _traceWriter.WriteLine("received: " + data); }; await _connection.Start(); await _connection.Send(new { Type = "sendToMe", Content = "Hello World!" }); }
private void RunRawConnection(string serverUrl) { string url = serverUrl + "raw-connection"; var connection = new Connection(url); connection.TraceWriter = _traceWriter; connection.Start().Wait(); connection.TraceWriter.WriteLine("transport.Name={0}", connection.Transport.Name); connection.Send(new { type = 1, value = "first message" }).Wait(); connection.Send(new { type = 1, value = "second message" }).Wait(); }
public ActionResult Test(string input) { // Connect to the service var connection = new Connection("http://squarepegio.apphb.com/echo"); // Print the message when it comes in connection.Received += data => Console.WriteLine(data); // Start the connection connection.Start().Wait(); connection.Send(input).Wait(); return null; }
private async Task RunPersistentConnection(string url, HttpClient httpClient, CookieContainer cookieContainer, string requestVerificationToken) { _traceWriter.WriteLine(); _traceWriter.WriteLine("*** Persistent Connection ***"); using (var connection = new Connection(url + "echo")) { connection.CookieContainer = cookieContainer; connection.TraceWriter = _traceWriter; connection.Received += (data) => connection.TraceWriter.WriteLine("Received: " + data); connection.Error += (exception) => connection.TraceWriter.WriteLine(string.Format("Error: {0}: {1}" + exception.GetType(), exception.Message)); await connection.Start(GetClientTransport()); await connection.Send("sending to AuthorizeEchoConnection"); await Task.Delay(TimeSpan.FromSeconds(2)); } }
static void Main(string[] args) { // Connect to the service var connection = new Connection("http://localhost:6820/echo"); // Print the message when it comes in connection.Received += data => Console.WriteLine(data); // Start the connection connection.Start().Wait(); string line = null; while ((line = Console.ReadLine()) != null) { // Send a message to the server connection.Send(line).Wait(); } }
public static void Start() { const string url = "http://*****:*****@ {1}",connection.Url,DateTime.Now)); //Just a sample to test the concept Thread.Sleep(2000); } } catch (Exception exception) { Console.WriteLine(exception); } } })); Console.ReadLine(); //server.Dispose(); }
private async Task RunAuth(string serverUrl) { string url = serverUrl + "cookieauth"; var handler = new HttpClientHandler(); handler.CookieContainer = new CookieContainer(); using (var httpClient = new HttpClient(handler)) { var content = string.Format("UserName={0}&Password={1}", "user", "password"); var response = httpClient.PostAsync(url + "/Account/Login", new StringContent(content, Encoding.UTF8, "application/x-www-form-urlencoded")).Result; } var connection = new Connection(url + "/echo"); connection.TraceWriter = _traceWriter; connection.Received += (data) => connection.TraceWriter.WriteLine(data); #if !ANDROID && !iOS connection.CookieContainer = handler.CookieContainer; #endif await connection.Start(); await connection.Send("sending to AuthenticatedEchoConnection"); var hubConnection = new HubConnection(url); hubConnection.TraceWriter = _traceWriter; #if !ANDROID && !iOS hubConnection.CookieContainer = handler.CookieContainer; #endif var hubProxy = hubConnection.CreateHubProxy("AuthHub"); hubProxy.On<string, string>("invoked", (connectionId, date) => hubConnection.TraceWriter.WriteLine("connectionId={0}, date={1}", connectionId, date)); await hubConnection.Start(); hubConnection.TraceWriter.WriteLine("transport.Name={0}", hubConnection.Transport.Name); await hubProxy.Invoke("InvokedFromClient"); }
private void RunBasicAuth(string serverUrl) { string url = serverUrl + "basicauth"; var connection = new Connection(url + "/echo"); connection.TraceWriter = _traceWriter; connection.Received += (data) => connection.TraceWriter.WriteLine(data); connection.Credentials = new NetworkCredential("user", "password"); connection.Start().Wait(); connection.Send("sending to AuthenticatedEchoConnection").Wait(); var hubConnection = new HubConnection(url); hubConnection.TraceWriter = _traceWriter; hubConnection.Credentials = new NetworkCredential("user", "password"); var hubProxy = hubConnection.CreateHubProxy("AuthHub"); hubProxy.On<string, string>("invoked", (connectionId, date) => hubConnection.TraceWriter.WriteLine("connectionId={0}, date={1}", connectionId, date)); hubConnection.Start().Wait(); hubConnection.TraceWriter.WriteLine("transport.Name={0}", hubConnection.Transport.Name); hubProxy.Invoke("InvokedFromClient").Wait(); }
private void RunAuth(string serverUrl) { string url = serverUrl + "cookieauth"; var cookieContainer = new CookieContainer(); var request = (HttpWebRequest)HttpWebRequest.Create(url + "/Account/Login"); request.CookieContainer = cookieContainer; request.ContentType = "application/x-www-form-urlencoded"; request.Method = "POST"; var content = string.Format("UserName={0}&Password={1}", "user", "password"); byte[] bytedata = Encoding.UTF8.GetBytes(content); request.ContentLength = bytedata.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(bytedata, 0, bytedata.Length); requestStream.Close(); var response = request.GetResponse(); var connection = new Connection(url + "/echo"); connection.TraceWriter = _traceWriter; connection.Received += (data) => connection.TraceWriter.WriteLine(data); connection.CookieContainer = cookieContainer; connection.Start().Wait(); connection.Send("sending to AuthenticatedEchoConnection").Wait(); var hubConnection = new HubConnection(url); hubConnection.TraceWriter = _traceWriter; hubConnection.CookieContainer = cookieContainer; var hubProxy = hubConnection.CreateHubProxy("AuthHub"); hubProxy.On<string, string>("invoked", (connectionId, date) => hubConnection.TraceWriter.WriteLine("connectionId={0}, date={1}", connectionId, date)); hubConnection.Start().Wait(); hubConnection.TraceWriter.WriteLine("transport.Name={0}", hubConnection.Transport.Name); hubProxy.Invoke("InvokedFromClient").Wait(); }
private static void BroadcastFive(MemoryHost host) { var connection = new Client.Connection("http://samples/Raw-connection"); connection.Error += e => { Console.Error.WriteLine("========ERROR=========="); Console.Error.WriteLine(e.GetBaseException().ToString()); Console.Error.WriteLine("======================="); }; connection.Start(new Client.Transports.ServerSentEventsTransport(host)).Wait(); try { for (int i = 0; i < 5; i++) { var payload = new { type = MessageType.Broadcast, value = "message " + i.ToString() }; connection.Send(payload).Wait(); } } catch (Exception ex) { Console.Error.WriteLine("========ERROR=========="); Console.Error.WriteLine(ex.GetBaseException().ToString()); Console.Error.WriteLine("======================="); } finally { connection.Stop(); } }
public static IDisposable ClientGroupsSyncWithServerGroupsOnReconnectLongPolling() { var host = new MemoryHost(); host.Configure(app => { var config = new ConnectionConfiguration() { Resolver = new DefaultDependencyResolver() }; app.MapConnection<MyRejoinGroupConnection>("/groups", config); var configuration = config.Resolver.Resolve<IConfigurationManager>(); configuration.KeepAlive = null; configuration.ConnectionTimeout = TimeSpan.FromSeconds(1); }); var connection = new Client.Connection("http://foo/groups"); var inGroupOnReconnect = new List<bool>(); var wh = new ManualResetEventSlim(); connection.Received += message => { Console.WriteLine(message); wh.Set(); }; connection.Reconnected += () => { connection.Send(new { type = 3, group = "test", message = "Reconnected" }).Wait(); }; connection.Start(new Client.Transports.LongPollingTransport(host)).Wait(); // Join the group connection.Send(new { type = 1, group = "test" }).Wait(); Thread.Sleep(TimeSpan.FromSeconds(10)); if (!wh.Wait(TimeSpan.FromSeconds(10))) { Debugger.Break(); } Console.WriteLine(inGroupOnReconnect.Count > 0); Console.WriteLine(String.Join(", ", inGroupOnReconnect.Select(b => b.ToString()))); connection.Stop(); return host; }