public virtual void OnWsMessage(object source, SpeckleEventArgs e) { try { AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, e.EventObject.args.eventType + "received at " + DateTime.Now + " from " + e.EventObject.senderId); switch ((string)e.EventObject.args.eventType) { case "get-definition-io": if (EnableRemoteControl == false) { return; } Dictionary <string, object> message = new Dictionary <string, object>(); message["eventType"] = "get-def-io-response"; message["controllers"] = DefaultSpeckleInputs; message["outputs"] = DefaultSpeckleOutputs; Client.SendMessage(e.EventObject.senderId, message); break; case "compute-request": if (EnableRemoteControl == true) { var requestClientId = (string)e.EventObject.senderId; if (JobQueue.Contains(requestClientId)) { JobQueue[requestClientId] = e.EventObject.args.requestParameters; } else { JobQueue.Add(requestClientId, e.EventObject.args.requestParameters); } AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, Document.SolutionState.ToString()); if (JobQueue.Count == 1) // means we just added one, so we need to start the solve loop { Rhino.RhinoApp.MainApplicationWindow.Invoke(ExpireComponentAction); } } else { Dictionary <string, object> computeMessage = new Dictionary <string, object>(); computeMessage["eventType"] = "compute-request-error"; computeMessage["response"] = "Remote control is disabled for this sender"; Client.SendMessage(e.EventObject.senderId, computeMessage); } break; default: Log += DateTime.Now.ToString("dd:HH:mm:ss") + " Defaulted, could not parse event. \n"; break; } } catch { } Debug.WriteLine("[Gh Sender] Got a volatile message. Extend this class and implement custom protocols at ease."); }
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(); }