public static void Main(string[] args) { ManualResetEvent Terminated = new ManualResetEvent(false); Initialize(); Console.Out.WriteLine("Move the mouse to move the pointer on the screen."); Console.Out.WriteLine("Press left mouse button while moving to draw."); Console.Out.WriteLine("Press the ESC key to close the application."); Console.Out.WriteLine("You will be able to see what others draw as well."); OnKeyDown += (sender, e) => { if (e.Key == Key.Escape || (e.Key == Key.C && e.Control)) { Terminated.Set(); } }; FillRectangle(0, 0, ScreenWidth, ScreenHeight, C64Colors.Blue); int PointerTexture = AddSpriteTexture(GetResourceBitmap("Pointer.png"), System.Drawing.Color.FromArgb(0, 0, 255), true); Point P = GetMousePointer(); Point LastP = P; Sprite Pointer = CreateSprite(P.X, P.Y, PointerTexture); Random Rnd = new System.Random(); int R = Rnd.Next(128, 255); int G = Rnd.Next(128, 255); int B = Rnd.Next(128, 255); Color Color = Color.FromArgb(R, G, B); bool Draw = false; BinaryOutput Payload; using (MqttConnection MqttConnection = ConnectToMqttServer("iot.eclipse.org", false, string.Empty, string.Empty)) { WriteLine("<" + MqttConnection.State.ToString() + ">", C64Colors.LightGreen); MqttConnection.TrustServer = true; MqttConnection.OnConnectionError += (sender, ex) => { WriteLine("Unable to connect:", C64Colors.Red); }; MqttConnection.OnError += (sender, ex) => { WriteLine(ex.Message, C64Colors.Red); }; MqttConnection.OnStateChanged += (sender, state) => { WriteLine("<" + MqttConnection.State.ToString() + ">", C64Colors.LightGreen); if (state == MqttState.Connected) { MqttConnection.SUBSCRIBE("RetroSharp/Examples/Networking/MultiUserDraw"); } }; OnMouseMove += (sender, e) => { P = e.Position; Pointer.SetPosition(P); int DX = P.X - RasterWidth / 2; int DY = P.Y - RasterHeight / 2; Pointer.Angle = 90 + 22.5 + System.Math.Atan2(DY, DX) * 180 / System.Math.PI; if (Draw) { Payload = new BinaryOutput(); Payload.WriteString(MqttConnection.ClientId); Payload.WriteInt(LastP.X); Payload.WriteInt(LastP.Y); Payload.WriteInt(P.X); Payload.WriteInt(P.Y); Payload.WriteColor(Color); MqttConnection.PUBLISH("RetroSharp/Examples/Networking/MultiUserDraw", MqttQualityOfService.AtMostOne, false, Payload); DrawLine(LastP.X, LastP.Y, P.X, P.Y, Color); } LastP = P; }; OnMouseDown += (sender, e) => { Draw = e.LeftButton; }; OnMouseUp += (sender, e) => { Draw = e.LeftButton; }; MqttConnection.OnContentReceived += (sender, Content) => { BinaryInput Input = Content.DataInput; string ClientId = Input.ReadString(); if (ClientId != MqttConnection.ClientId) { int X1 = (int)Input.ReadInt(); int Y1 = (int)Input.ReadInt(); int X2 = (int)Input.ReadInt(); int Y2 = (int)Input.ReadInt(); Color cl = Input.ReadColor(); DrawLine(X1, Y1, X2, Y2, cl); } }; while (!Terminated.WaitOne(1000)) { ; } } Terminate(); }
public static void Main(string[] args) { Initialize(); Console.Out.Write("Host Name (default iot.eclipse.org): "); string Host = Console.In.ReadLine(); if (string.IsNullOrEmpty(Host)) { Console.Out.WriteLine("Using iot.eclipse.org."); Host = "iot.eclipse.org"; } Console.Out.WriteLine(); Console.Out.Write("Port Number (default 1883): "); string s = Console.In.ReadLine(); int Port; if (string.IsNullOrEmpty(s)) { Console.Out.WriteLine("Using port 1883."); Port = 1883; } else { Port = int.Parse(s); } Console.Out.WriteLine(); BinaryOutput Payload; int PacketsLeft = NrTestsPerQoS; using (MqttConnection MqttConnection = ConnectToMqttServer("iot.eclipse.org", Port, string.Empty, string.Empty)) { WriteLine("<" + MqttConnection.State.ToString() + ">", C64Colors.LightGreen); MqttConnection.TrustServer = true; MqttConnection.OnConnectionError += (sender, ex) => { WriteLine("Unable to connect:", C64Colors.Red); }; MqttConnection.OnError += (sender, ex) => { WriteLine(ex.Message, C64Colors.Red); }; MqttConnection.OnContentReceived += (sender, Content) => { string ClientId = Content.DataInput.ReadString(); if (ClientId == sender.ClientId) { DateTime TP = Content.DataInput.ReadDateTime(); MqttQualityOfService QoS = (MqttQualityOfService)Content.DataInput.ReadByte(); Console.Out.WriteLine("Latency: " + (DateTime.Now - TP).TotalMilliseconds + " ms (" + QoS.ToString() + ")"); bool Resend; if (--PacketsLeft > 0) { Resend = true; } else if (QoS < MqttQualityOfService.ExactlyOne) { QoS = (MqttQualityOfService)((int)QoS + 1); PacketsLeft = NrTestsPerQoS; Resend = true; } else { Resend = false; } if (Resend) { Payload = new BinaryOutput(); Payload.WriteString(MqttConnection.ClientId); Payload.WriteDateTime(DateTime.Now); Payload.WriteByte((byte)QoS); MqttConnection.PUBLISH("RetroSharp/Examples/Networking/Latency", QoS, false, Payload); } else { Console.Out.WriteLine("Press ENTER to continue."); } } }; MqttConnection.OnStateChanged += (sender, state) => { WriteLine("<" + MqttConnection.State.ToString() + ">", C64Colors.LightGreen); if (state == MqttState.Connected) { MqttConnection.SUBSCRIBE("RetroSharp/Examples/Networking/Latency"); Payload = new BinaryOutput(); Payload.WriteString(MqttConnection.ClientId); Payload.WriteDateTime(DateTime.Now); Payload.WriteByte((byte)MqttQualityOfService.AtMostOne); MqttConnection.PUBLISH("RetroSharp/Examples/Networking/Latency", MqttQualityOfService.AtMostOne, false, Payload); } }; Console.In.ReadLine(); } Terminate(); }
public static void Main(string[] args) { Initialize(); WriteLine("What is your name?", C64Colors.LightBlue); string Name = Console.In.ReadLine(); BinaryOutput Payload; WriteLine("Hello " + Name + ".", C64Colors.LightBlue); WriteLine("Strings entered below will be seen by everybody running the application.", C64Colors.LightBlue); WriteLine("Enter an empty string to close the application.", C64Colors.LightBlue); WriteLine(new string('-', ConsoleWidth), C64Colors.LightBlue); using (MqttConnection MqttConnection = ConnectToMqttServer("iot.eclipse.org", true, string.Empty, string.Empty)) { WriteLine("<" + MqttConnection.State.ToString() + ">", C64Colors.LightGreen); MqttConnection.TrustServer = true; MqttConnection.OnConnectionError += (sender, ex) => { WriteLine("Unable to connect:", C64Colors.Red); }; MqttConnection.OnError += (sender, ex) => { WriteLine(ex.Message, C64Colors.Red); }; MqttConnection.OnStateChanged += (sender, state) => { WriteLine("<" + MqttConnection.State.ToString() + ">", C64Colors.LightGreen); if (state == MqttState.Connected) { MqttConnection.SUBSCRIBE("RetroSharp/Examples/Networking/MultiUserChat"); Payload = new BinaryOutput(); Payload.WriteString(MqttConnection.ClientId); Payload.WriteString(Name); Payload.WriteByte(0); MqttConnection.PUBLISH("RetroSharp/Examples/Networking/MultiUserChat", MqttQualityOfService.AtLeastOne, false, Payload); } }; MqttConnection.OnContentReceived += (sender, Content) => { string ClientId = Content.DataInput.ReadString(); if (ClientId != sender.ClientId) { string Author = Content.DataInput.ReadString(); byte Command = Content.DataInput.ReadByte(); switch (Command) { case 0: WriteLine("<" + Author + " enters the room.>", C64Colors.LightGreen); break; case 1: string Text = Content.DataInput.ReadString(); WriteLine(Author + ": " + Text, C64Colors.LightBlue); break; case 2: WriteLine("<" + Author + " left the room.>", C64Colors.LightGreen); break; } } }; while (true) { string s = Console.In.ReadLine(); if (string.IsNullOrEmpty(s)) { break; } Payload = new BinaryOutput(); Payload.WriteString(MqttConnection.ClientId); Payload.WriteString(Name); Payload.WriteByte(1); Payload.WriteString(s); MqttConnection.PUBLISH("RetroSharp/Examples/Networking/MultiUserChat", MqttQualityOfService.AtLeastOne, false, Payload); } MqttConnection.UNSUBSCRIBE("RetroSharp/Examples/Networking/MultiUserChat"); int PacketIdentifier = 0; ManualResetEvent Terminated = new ManualResetEvent(false); MqttConnection.OnPublished += (sender, e) => { if (PacketIdentifier == e) { Terminated.Set(); } }; Payload = new BinaryOutput(); Payload.WriteString(MqttConnection.ClientId); Payload.WriteString(Name); Payload.WriteByte(2); PacketIdentifier = MqttConnection.PUBLISH("RetroSharp/Examples/Networking/MultiUserChat", MqttQualityOfService.AtLeastOne, false, Payload); Terminated.WaitOne(5000); } Terminate(); }