static void Main(string[] args) { Console.WriteLine("Hello Smelly Sockets"); SelectedAccount = SpkConsole.Program.GetAccount(); var spkClient_A = new SpeckleApiClient(SelectedAccount.RestApi, false, "console application"); var spkClient_B = new SpeckleApiClient(SelectedAccount.RestApi, false, "console application"); spkClient_A.AuthToken = SelectedAccount.Token; spkClient_B.AuthToken = SelectedAccount.Token; //gen streamid DummyStreamId = spkClient_A.StreamCreateAsync(new SpeckleStream() { Name = "WS Test" }).Result.Resource.StreamId; Console.WriteLine($"Created dummy stream: {DummyStreamId}. Press any key to continue stuff."); // Add event handlers and setup streamId on both ws clients. The event handlers just spit out what they get. spkClient_A.StreamId = DummyStreamId; spkClient_A.SetupWebsocket(); spkClient_A.OnWsMessage += SpkClient_A_OnWsMessage; spkClient_B.StreamId = DummyStreamId; spkClient_B.SetupWebsocket(); spkClient_B.OnWsMessage += SpkClient_B_OnWsMessage; Console.WriteLine("Waiting for 200ms, ensure connection actually happened. This is an issue with core, it doesn't expose a 'onwsconnection' event :/"); Thread.Sleep(200); // Flop them in a room - this is important if you want to broadcast messages. spkClient_A.JoinRoom("stream", DummyStreamId); spkClient_B.JoinRoom("stream", DummyStreamId); // Same hack as above. Thread.Sleep(200); // Send some dummy broadcasts spkClient_A.BroadcastMessage("stream", DummyStreamId, new { customEventType = "update-mesh", data = "42" }); spkClient_A.BroadcastMessage("stream", DummyStreamId, new { customEventType = "update-mesh-other", data = "wow" }); spkClient_B.BroadcastMessage("stream", DummyStreamId, new { customEventType = "update-mesh-other", data = "wow" }); spkClient_B.SendMessage(spkClient_A.ClientId, new { what = "This is a direct, 1-1 message from B to A." }); Console.WriteLine("Press any key to continue testing the barebones approach!"); BareBonesApproach(); Console.WriteLine("Done. Press any key to continue."); spkClient_A.StreamDeleteAsync(DummyStreamId); Console.ReadLine(); }
/// <summary> /// Initializes sender. /// </summary> /// <param name="streamID">Stream ID of stream. If no stream ID is given, a new stream is created.</param> /// <param name="streamName">Stream name</param> /// <returns>Task</returns> public async Task InitializeSender(string streamID = "", string clientID = "", string streamName = "") { apiClient.AuthToken = apiToken; if (string.IsNullOrEmpty(clientID)) { HelperFunctions.tryCatchWithEvents(() => { var streamResponse = apiClient.StreamCreateAsync(new SpeckleStream()).Result; apiClient.Stream = streamResponse.Resource; apiClient.StreamId = streamResponse.Resource.StreamId; }, "", "Unable to create stream on the server"); HelperFunctions.tryCatchWithEvents(() => { var clientResponse = apiClient.ClientCreateAsync(new AppClient() { DocumentName = Path.GetFileNameWithoutExtension(GSA.GsaApp.gsaProxy.FilePath), DocumentType = "GSA", Role = "Sender", StreamId = this.StreamID, Online = true, }).Result; apiClient.ClientId = clientResponse.Resource._id; }, "", "Unable to create client on the server"); } else { HelperFunctions.tryCatchWithEvents(() => { var streamResponse = apiClient.StreamGetAsync(streamID, null).Result; apiClient.Stream = streamResponse.Resource; apiClient.StreamId = streamResponse.Resource.StreamId; }, "", "Unable to get stream response"); HelperFunctions.tryCatchWithEvents(() => { var clientResponse = apiClient.ClientUpdateAsync(clientID, new AppClient() { DocumentName = Path.GetFileNameWithoutExtension(GSA.GsaApp.gsaProxy.FilePath), Online = true, }).Result; apiClient.ClientId = clientID; }, "", "Unable to update client on the server"); } apiClient.Stream.Name = streamName; HelperFunctions.tryCatchWithEvents(() => { apiClient.SetupWebsocket(); }, "", "Unable to set up web socket"); HelperFunctions.tryCatchWithEvents(() => { apiClient.JoinRoom("stream", streamID); }, "", "Uable to join web socket"); }
/// <summary> /// <para>Creates a stream.</para> /// <para>Please note this is not the best way to do it for large payloads!</para> /// </summary> /// <param name="account"></param> /// <returns></returns> static string CreateStream(Account account) { Console.WriteLine("Hello Speckle! We will now create a sample stream."); Console.WriteLine("Please enter a stream name:"); var name = Console.ReadLine(); var myStream = new SpeckleStream() { Objects = new List <SpeckleObject>(), Name = name != "" ? name : "Console test stream", Description = "This stream was created from a .net console program. Easy peasy.", Tags = new List <string> { "example", "console-test" } }; Console.WriteLine("Creating and converting sample data... "); var sampleSize = 10; var myNodes = new List <Node>(); for (int i = 0; i < sampleSize; i++) { myNodes.Add(new Node { id = i, x = i, y = i + 1, z = i + 2 }); myStream.Objects.Add(myNodes[i]); Console.WriteLine("Added " + i + " nodes"); } var myQuads = new List <Quad>(); for (int i = 0; i < sampleSize; i++) { myQuads.Add(new Quad { id = i, nodes = myNodes.GetRange(0, i).ToArray(), vx = 42, iteration = 42, tau = 1337 // obviously bogus numbers }); myStream.Objects.Add(myQuads[i]); Console.WriteLine("Added " + i + " quads"); } Console.WriteLine("Done.\n"); Console.WriteLine(String.Format("Saving stream to {0} using {1}'s account. This might take a bit.", account.RestApi, account.Email)); // create an api client var client = new SpeckleApiClient(account.RestApi, false, "console_app"); client.AuthToken = account.Token; try { // save the stream. var result = client.StreamCreateAsync(myStream).Result; // profit Console.WriteLine(String.Format("Succesfully created a stream! It's id is {0}. Rock on! Check it out at {1}/streams/{0}", result.Resource.StreamId, account.RestApi)); Console.WriteLine("Press any key to continue."); System.Diagnostics.Process.Start(String.Format("{1}/streams/{0}", result.Resource.StreamId, account.RestApi)); Console.ReadLine(); return(result.Resource.StreamId); } catch (Exception e) { Console.WriteLine("Bummer - something went wrong:"); Console.WriteLine(e.Message); Console.WriteLine("Press any key to continue."); Console.ReadLine(); return(null); } }
static async Task TestStreams(SpeckleApiClient myClient) { string streamId = "lol"; string secondStreamId = "hai"; var myPoint = new SpecklePoint() { Value = new List <double>() { 1, 2, 3 } }; var mySecondPoint = new SpecklePoint() { Value = new List <double>() { 23, 33, 12 } }; var myCircle = new SpeckleCircle() { Radius = 21 }; myPoint.Properties = new Dictionary <string, object>(); myPoint.Properties.Add("Really", mySecondPoint); myCircle.Properties = new Dictionary <string, object>(); myCircle.Properties.Add("a property", "Hello!"); myCircle.Properties.Add("point", myPoint); SpeckleStream myStream = new SpeckleStream() { Name = "Hello World My Little Stream", Objects = new List <SpeckleObject>() { myCircle, myPoint } }; SpeckleStream secondStream = new SpeckleStream() { Name = "Second Little Stream", Objects = new List <SpeckleObject>() { myCircle, mySecondPoint } }; Console.WriteLine(); try { Console.WriteLine("Creating a stream."); var Response = await myClient.StreamCreateAsync(myStream); Console.WriteLine("OK: " + Response.Resource.ToJson()); streamId = Response.Resource.StreamId; } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine(); try { Console.WriteLine("Creating a second stream."); var Response = await myClient.StreamCreateAsync(secondStream); Console.WriteLine("OK: " + Response.Resource.ToJson()); secondStreamId = Response.Resource.StreamId; } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine(); try { Console.WriteLine("Diffing two streams!"); var Response = await myClient.StreamDiffAsync(streamId, secondStreamId); Console.WriteLine("OK: " + Response.Message); } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine(); try { Console.WriteLine("Getting a stream."); var Response = await myClient.StreamGetAsync(streamId, null); Console.WriteLine("OK: " + Response.Resource.ToJson()); } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine(); try { Console.WriteLine("Getting a stream's objects."); var Response = await myClient.StreamGetObjectsAsync(streamId, null); Console.WriteLine("OK: " + Response.Resources.Count); } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine(); try { Console.WriteLine("Updating a stream."); var Response = await myClient.StreamUpdateAsync(streamId, new SpeckleStream() { Name = "I hate api testing", ViewerLayers = new List <object>() { new { test = "test" } } }); Console.WriteLine("OK: " + Response.Message); } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine(); try { Console.WriteLine("Getting a stream field."); var Response = await myClient.StreamGetAsync(streamId, "fields=viewerLayers,name,owner"); Console.WriteLine("OK: " + Response.Resource.ToJson()); } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine(); try { Console.WriteLine("Getting all users's streams."); var Response = await myClient.StreamsGetAllAsync(); Console.WriteLine("OK: " + Response.Resources.Count); } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine(); try { Console.WriteLine("Cloning a stream."); var Response = await myClient.StreamCloneAsync(streamId); Console.WriteLine("OK: " + Response.Message); } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine(); try { Console.WriteLine("Deleting a stream: " + streamId); var Response = await myClient.StreamDeleteAsync(streamId); Console.WriteLine("OK: " + Response.Message); } catch (Exception e) { Console.WriteLine(e.Message); } }
/// <summary> /// Creates a stream with many objects. Use this as a reference for how to save arbitrarily large numbers of objects. /// </summary> /// <param name="numObjects">how many objects to create.</param> static void CreateStreamWithManyObjects(int numObjects) { if (numObjects > 50000) { Console.WriteLine("Let's think about this: is this the best way to do it? Y/N"); var answer = Console.ReadLine(); if (answer == "N") { return; } else { Console.WriteLine("oh well, ok... will go ahead."); } } var nodes = new List <Node>(); for (int i = 0; i < numObjects; i++) { nodes.Add(new Node { x = i, y = i % 2, z = i % 3 }); } var account = GetAccount(); var savedObjects = SaveManyObjects(account, nodes); var client = new SpeckleApiClient(account.RestApi, false, "console_app"); client.AuthToken = account.Token; Console.WriteLine("Please enter a stream name:"); var name = Console.ReadLine(); var myStream = new SpeckleStream() { Objects = savedObjects.Cast <SpeckleObject>().ToList(), Name = name != "" ? name : "Console test stream", Description = "This stream was created from a .net console program. Easy peasy.", Tags = new List <string> { "example", "console-test" }, }; try { // save the stream. var result = client.StreamCreateAsync(myStream).Result; // profit Console.WriteLine(String.Format("Succesfully created a stream! It's id is {0}. Rock on! Check it out at {1}/streams/{0}", result.Resource.StreamId, account.RestApi)); Console.WriteLine("Press any key to continue."); System.Diagnostics.Process.Start(String.Format("{1}/streams/{0}", result.Resource.StreamId, account.RestApi)); Console.ReadLine(); } catch (Exception e) { Console.WriteLine("Bummer - something went wrong:"); Console.WriteLine(e.Message); Console.WriteLine("Press any key to continue."); Console.ReadLine(); } }