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"); }