using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace YourWpfApp
{
public partial class MainWindow : Window
{
private HttpListener _listener;
public MainWindow()
{
InitializeComponent();
StartHttpListener();
}
private async void StartHttpListener()
{
_listener = new HttpListener();
_listener.Prefixes.Add("http://your-ip-address:5000/"); //If I use my machines IP address, how figma plugin access it?
_listener.Start();
Console.WriteLine("Listening...");
while (true)
{
var context = await _listener.GetContextAsync();
ProcessRequest(context);
}
}
private void ProcessRequest(HttpListenerContext context)
{
string responseString = "{"message": "Hello from WPF Server!"}";
context.Response.StatusCode = (int)HttpStatusCode.OK;
context.Response.ContentType = "application/json";
// Read incoming data
using (var reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding))
{
string requestData = reader.ReadToEnd();
Console.WriteLine("Received data: " + requestData);
}
// Send response back
byte[] buffer = Encoding.UTF8.GetBytes(responseString);
context.Response.ContentLength64 = buffer.Length;
using (var output = context.Response.OutputStream)
{
output.Write(buffer, 0, buffer.Length);
}
context.Response.Close();
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
_listener.Stop();
_listener.Close();
}
}
}
my sample c# code
My sample Figma plugin code
async function sendDataToCSharp(data) {
try {
const response = await fetch('http://192.168.1.100:5000/', { // Use the internal IP
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
const responseData = await response.json();
console.log(responseData);
} catch (error) {
console.error('Error:', error);
}
}
But how Figma plugin access my machine? I am using my machine in my company’s network as well.