diff --git a/public/usage-examples/networking/send_message_to_connection-1-example-oop.cs b/public/usage-examples/networking/send_message_to_connection-1-example-oop.cs new file mode 100644 index 000000000..69e3e832f --- /dev/null +++ b/public/usage-examples/networking/send_message_to_connection-1-example-oop.cs @@ -0,0 +1,119 @@ +using SplashKitSDK; + +namespace SendMessageToConnectionExample +{ + public class Program + { + public static void Main() + { + // Open a window for the telemetry hub display + SplashKit.OpenWindow("UDP Telemetry Hub", 800, 600); + + // Set up a UDP server to receive telemetry data + ServerSocket hubServer = SplashKit.CreateServer("hub", 5000, ConnectionType.UDP); + + // Set up a UDP connection to send telemetry data to the hub + Connection senderConn = SplashKit.OpenConnection("sender", "127.0.0.1", 5000, ConnectionType.UDP); + + // Simulated sprite position (sender side) + double spritePosX = 400.0; + double spritePosY = 300.0; + double velocityX = 2.0; + double velocityY = 1.5; + + // Received position (hub display side) + double receivedX = 0.0; + double receivedY = 0.0; + bool hasData = false; + + // Send an initial handshake ping + senderConn.SendMessage("PING"); + + while (!SplashKit.QuitRequested()) + { + SplashKit.ProcessEvents(); + + // Update simulated sprite position with bouncing + spritePosX += velocityX; + spritePosY += velocityY; + + // Bounce off window edges + if (spritePosX <= 0 || spritePosX >= 780) + { + velocityX = -velocityX; + } + if (spritePosY <= 80 || spritePosY >= 580) + { + velocityY = -velocityY; + } + + // Format position data as a string and send via UDP + string posData = "POS:" + ((int)spritePosX).ToString() + "," + ((int)spritePosY).ToString(); + senderConn.SendMessage(posData); + + // Check for incoming UDP packets on the hub server + SplashKit.CheckNetworkActivity(); + + // Process any received messages + if (SplashKit.HasMessages()) + { + // Read the incoming message and extract its payload + Message msg = SplashKit.ReadMessage(); + string payload = msg.Data; + + // Parse the position data from the payload + if (payload.Length >= 4 && payload.Substring(0, 4) == "POS:") + { + string coords = payload.Substring(4); + int commaPos = coords.IndexOf(","); + receivedX = double.Parse(coords.Substring(0, commaPos)); + receivedY = double.Parse(coords.Substring(commaPos + 1)); + hasData = true; + } + + SplashKit.CloseMessage(msg); + } + + // Render the hub display + SplashKit.ClearScreen(Color.Black); + + // Draw header panel + SplashKit.FillRectangle(SplashKit.RGBAColor(30, 30, 50, 255), 0, 0, 800, 70); + SplashKit.DrawText("UDP Telemetry Hub", Color.White, 20, 10); + SplashKit.DrawText("Protocol: UDP | Port: 5000 | Status: LIVE", Color.Green, 20, 35); + + // Draw the sender indicator (what is being sent) + SplashKit.FillCircle(SplashKit.RGBAColor(50, 120, 200, 150), spritePosX, spritePosY, 10); + SplashKit.DrawText("SENDER", SplashKit.RGBAColor(50, 120, 200, 200), spritePosX - 20, spritePosY - 20); + + // Draw the received position indicator (what the hub received) + if (hasData) + { + SplashKit.FillCircle(Color.Yellow, receivedX, receivedY, 6); + SplashKit.DrawCircle(SplashKit.RGBAColor(255, 255, 0, 100), receivedX, receivedY, 15); + SplashKit.DrawText("HUB", Color.Yellow, receivedX - 10, receivedY + 18); + + // Display telemetry readout + SplashKit.FillRectangle(SplashKit.RGBAColor(20, 20, 40, 200), 560, 80, 230, 100); + SplashKit.DrawRectangle(SplashKit.RGBAColor(0, 200, 100, 150), 560, 80, 230, 100); + SplashKit.DrawText("Telemetry Readout", Color.Green, 580, 90); + SplashKit.DrawText("X: " + ((int)receivedX).ToString(), Color.White, 580, 115); + SplashKit.DrawText("Y: " + ((int)receivedY).ToString(), Color.White, 580, 140); + } + + // Draw a dashed connection line between sender and hub + if (hasData) + { + SplashKit.DrawLine(SplashKit.RGBAColor(0, 200, 100, 80), spritePosX, spritePosY, receivedX, receivedY); + } + + SplashKit.RefreshScreen(60); + } + + // Clean up networking resources + SplashKit.CloseAllConnections(); + SplashKit.CloseAllServers(); + SplashKit.CloseAllWindows(); + } + } +} diff --git a/public/usage-examples/networking/send_message_to_connection-1-example-top-level.cs b/public/usage-examples/networking/send_message_to_connection-1-example-top-level.cs new file mode 100644 index 000000000..0c95cb75b --- /dev/null +++ b/public/usage-examples/networking/send_message_to_connection-1-example-top-level.cs @@ -0,0 +1,111 @@ +using SplashKitSDK; +using static SplashKitSDK.SplashKit; + +// Open a window for the telemetry hub display +OpenWindow("UDP Telemetry Hub", 800, 600); + +// Set up a UDP server to receive telemetry data +ServerSocket hubServer = CreateServer("hub", 5000, ConnectionType.UDP); + +// Set up a UDP connection to send telemetry data to the hub +Connection senderConn = OpenConnection("sender", "127.0.0.1", 5000, ConnectionType.UDP); + +// Simulated sprite position (sender side) +double spritePosX = 400.0; +double spritePosY = 300.0; +double velocityX = 2.0; +double velocityY = 1.5; + +// Received position (hub display side) +double receivedX = 0.0; +double receivedY = 0.0; +bool hasData = false; + +// Send an initial handshake ping +SendMessageTo("PING", senderConn); + +while (!QuitRequested()) +{ + ProcessEvents(); + + // Update simulated sprite position with bouncing + spritePosX += velocityX; + spritePosY += velocityY; + + // Bounce off window edges + if (spritePosX <= 0 || spritePosX >= 780) + { + velocityX = -velocityX; + } + if (spritePosY <= 80 || spritePosY >= 580) + { + velocityY = -velocityY; + } + + // Format position data as a string and send via UDP + string posData = "POS:" + ((int)spritePosX).ToString() + "," + ((int)spritePosY).ToString(); + SendMessageTo(posData, senderConn); + + // Check for incoming UDP packets on the hub server + CheckNetworkActivity(); + + // Process any received messages + if (HasMessages()) + { + // Read the incoming message and extract its payload + Message msg = ReadMessage(); + string payload = MessageData(msg); + + // Parse the position data from the payload + if (payload.Length >= 4 && payload.Substring(0, 4) == "POS:") + { + string coords = payload.Substring(4); + int commaPos = coords.IndexOf(","); + receivedX = double.Parse(coords.Substring(0, commaPos)); + receivedY = double.Parse(coords.Substring(commaPos + 1)); + hasData = true; + } + + CloseMessage(msg); + } + + // Render the hub display + ClearScreen(ColorBlack()); + + // Draw header panel + FillRectangle(RGBAColor(30, 30, 50, 255), 0, 0, 800, 70); + DrawText("UDP Telemetry Hub", ColorWhite(), 20, 10); + DrawText("Protocol: UDP | Port: 5000 | Status: LIVE", ColorGreen(), 20, 35); + + // Draw the sender indicator (what is being sent) + FillCircle(RGBAColor(50, 120, 200, 150), spritePosX, spritePosY, 10); + DrawText("SENDER", RGBAColor(50, 120, 200, 200), spritePosX - 20, spritePosY - 20); + + // Draw the received position indicator (what the hub received) + if (hasData) + { + FillCircle(ColorYellow(), receivedX, receivedY, 6); + DrawCircle(RGBAColor(255, 255, 0, 100), receivedX, receivedY, 15); + DrawText("HUB", ColorYellow(), receivedX - 10, receivedY + 18); + + // Display telemetry readout + FillRectangle(RGBAColor(20, 20, 40, 200), 560, 80, 230, 100); + DrawRectangle(RGBAColor(0, 200, 100, 150), 560, 80, 230, 100); + DrawText("Telemetry Readout", ColorGreen(), 580, 90); + DrawText("X: " + ((int)receivedX).ToString(), ColorWhite(), 580, 115); + DrawText("Y: " + ((int)receivedY).ToString(), ColorWhite(), 580, 140); + } + + // Draw a dashed connection line between sender and hub + if (hasData) + { + DrawLine(RGBAColor(0, 200, 100, 80), spritePosX, spritePosY, receivedX, receivedY); + } + + RefreshScreen(60); +} + +// Clean up networking resources +CloseAllConnections(); +CloseAllServers(); +CloseAllWindows(); diff --git a/public/usage-examples/networking/send_message_to_connection-1-example.cpp b/public/usage-examples/networking/send_message_to_connection-1-example.cpp new file mode 100644 index 000000000..cb27faa6f --- /dev/null +++ b/public/usage-examples/networking/send_message_to_connection-1-example.cpp @@ -0,0 +1,115 @@ +#include "splashkit.h" + +int main() +{ + // Open a window for the telemetry hub display + open_window("UDP Telemetry Hub", 800, 600); + + // Set up a UDP server to receive telemetry data + server_socket hub_server = create_server("hub", 5000, UDP); + + // Set up a UDP connection to send telemetry data to the hub + connection sender_conn = open_connection("sender", "127.0.0.1", 5000, UDP); + + // Simulated sprite position (sender side) + double sprite_pos_x = 400.0; + double sprite_pos_y = 300.0; + double velocity_x = 2.0; + double velocity_y = 1.5; + + // Received position (hub display side) + double received_x = 0.0; + double received_y = 0.0; + bool has_data = false; + + // Send an initial handshake ping + send_message_to(string("PING"), sender_conn); + + while (!quit_requested()) + { + process_events(); + + // Update simulated sprite position with bouncing + sprite_pos_x += velocity_x; + sprite_pos_y += velocity_y; + + // Bounce off window edges + if (sprite_pos_x <= 0 || sprite_pos_x >= 780) + { + velocity_x = -velocity_x; + } + if (sprite_pos_y <= 80 || sprite_pos_y >= 580) + { + velocity_y = -velocity_y; + } + + // Format position data as a string and send via UDP + string pos_data = "POS:" + std::to_string((int)sprite_pos_x) + "," + std::to_string((int)sprite_pos_y); + send_message_to(pos_data, sender_conn); + + // Check for incoming UDP packets on the hub server + check_network_activity(); + + // Process any received messages + if (has_messages()) + { + // Read the incoming message and extract its payload + message msg = read_message(); + string payload = message_data(msg); + + // Parse the position data from the payload + if (payload.substr(0, 4) == "POS:") + { + string coords = payload.substr(4); + int comma_pos = coords.find(","); + received_x = stod(coords.substr(0, comma_pos)); + received_y = stod(coords.substr(comma_pos + 1)); + has_data = true; + } + + close_message(msg); + } + + // Render the hub display + clear_screen(color_black()); + + // Draw header panel + fill_rectangle(rgba_color(30, 30, 50, 255), 0, 0, 800, 70); + draw_text("UDP Telemetry Hub", color_white(), 20, 10); + draw_text("Protocol: UDP | Port: 5000 | Status: LIVE", color_green(), 20, 35); + + // Draw the sender indicator (what is being sent) + fill_circle(rgba_color(50, 120, 200, 150), sprite_pos_x, sprite_pos_y, 10); + draw_text("SENDER", rgba_color(50, 120, 200, 200), sprite_pos_x - 20, sprite_pos_y - 20); + + // Draw the received position indicator (what the hub received) + if (has_data) + { + fill_circle(color_yellow(), received_x, received_y, 6); + draw_circle(rgba_color(255, 255, 0, 100), received_x, received_y, 15); + draw_text("HUB", color_yellow(), received_x - 10, received_y + 18); + + // Display telemetry readout + fill_rectangle(rgba_color(20, 20, 40, 200), 560, 80, 230, 100); + draw_rectangle(rgba_color(0, 200, 100, 150), 560, 80, 230, 100); + draw_text("Telemetry Readout", color_green(), 580, 90); + draw_text("X: " + std::to_string((int)received_x), color_white(), 580, 115); + draw_text("Y: " + std::to_string((int)received_y), color_white(), 580, 140); + } + + // Draw a dashed connection line between sender and hub + if (has_data) + { + draw_line(rgba_color(0, 200, 100, 80), sprite_pos_x, sprite_pos_y, received_x, received_y); + } + + refresh_screen(60); + } + + // Clean up networking resources + close_all_connections(); + close_all_servers(); + close_all_windows(); + + return 0; +} diff --git a/public/usage-examples/networking/send_message_to_connection-1-example.gif b/public/usage-examples/networking/send_message_to_connection-1-example.gif new file mode 100644 index 000000000..62703a1ce Binary files /dev/null and b/public/usage-examples/networking/send_message_to_connection-1-example.gif differ diff --git a/public/usage-examples/networking/send_message_to_connection-1-example.py b/public/usage-examples/networking/send_message_to_connection-1-example.py new file mode 100644 index 000000000..fc08496e6 --- /dev/null +++ b/public/usage-examples/networking/send_message_to_connection-1-example.py @@ -0,0 +1,96 @@ +from splashkit import * + +# Open a window for the telemetry hub display +open_window("UDP Telemetry Hub", 800, 600) + +# Set up a UDP server to receive telemetry data +hub_server = create_server_with_port_and_protocol("hub", 5000, ConnectionType.udp) + +# Set up a UDP connection to send telemetry data to the hub +sender_conn = open_connection_with_protocol("sender", "127.0.0.1", 5000, ConnectionType.udp) + +# Simulated sprite position (sender side) +sprite_pos_x = 400.0 +sprite_pos_y = 300.0 +velocity_x = 2.0 +velocity_y = 1.5 + +# Received position (hub display side) +received_x = 0.0 +received_y = 0.0 +has_data = False + +# Send an initial handshake ping +send_message_to_connection("PING", sender_conn) + +while not quit_requested(): + process_events() + + # Update simulated sprite position with bouncing + sprite_pos_x += velocity_x + sprite_pos_y += velocity_y + + # Bounce off window edges + if sprite_pos_x <= 0 or sprite_pos_x >= 780: + velocity_x = -velocity_x + if sprite_pos_y <= 80 or sprite_pos_y >= 580: + velocity_y = -velocity_y + + # Format position data as a string and send via UDP + pos_data = "POS:" + str(int(sprite_pos_x)) + "," + str(int(sprite_pos_y)) + send_message_to_connection(pos_data, sender_conn) + + # Check for incoming UDP packets on the hub server + check_network_activity() + + # Process any received messages + if has_messages(): + # Read the incoming message and extract its payload + msg = read_message() + payload = message_data(msg) + + # Parse the position data from the payload + if len(payload) >= 4 and payload[:4] == "POS:": + coords = payload[4:] + comma_pos = coords.index(",") + received_x = float(coords[:comma_pos]) + received_y = float(coords[comma_pos + 1:]) + has_data = True + + close_message(msg) + + # Render the hub display + clear_screen(color_black()) + + # Draw header panel + fill_rectangle(rgba_color(30, 30, 50, 255), 0, 0, 800, 70) + draw_text_no_font_no_size("UDP Telemetry Hub", color_white(), 20, 10) + draw_text_no_font_no_size("Protocol: UDP | Port: 5000 | Status: LIVE", color_green(), 20, 35) + + # Draw the sender indicator (what is being sent) + fill_circle(rgba_color(50, 120, 200, 150), sprite_pos_x, sprite_pos_y, 10) + draw_text_no_font_no_size("SENDER", rgba_color(50, 120, 200, 200), sprite_pos_x - 20, sprite_pos_y - 20) + + # Draw the received position indicator (what the hub received) + if has_data: + fill_circle(color_yellow(), received_x, received_y, 6) + draw_circle(rgba_color(255, 255, 0, 100), received_x, received_y, 15) + draw_text_no_font_no_size("HUB", color_yellow(), received_x - 10, received_y + 18) + + # Display telemetry readout + fill_rectangle(rgba_color(20, 20, 40, 200), 560, 80, 230, 100) + draw_rectangle(rgba_color(0, 200, 100, 150), 560, 80, 230, 100) + draw_text_no_font_no_size("Telemetry Readout", color_green(), 580, 90) + draw_text_no_font_no_size("X: " + str(int(received_x)), color_white(), 580, 115) + draw_text_no_font_no_size("Y: " + str(int(received_y)), color_white(), 580, 140) + + # Draw a dashed connection line between sender and hub + if has_data: + draw_line(rgba_color(0, 200, 100, 80), sprite_pos_x, sprite_pos_y, received_x, received_y) + + refresh_screen_with_target_fps(60) + +# Clean up networking resources +close_all_connections() +close_all_servers() +close_all_windows() diff --git a/public/usage-examples/networking/send_message_to_connection-1-example.txt b/public/usage-examples/networking/send_message_to_connection-1-example.txt new file mode 100644 index 000000000..30e8fcf02 --- /dev/null +++ b/public/usage-examples/networking/send_message_to_connection-1-example.txt @@ -0,0 +1 @@ +UDP Telemetry Hub