Skip to main content
Question

C# Figma communication

  • October 21, 2024
  • 2 replies
  • 36 views

Nidhin_Kuttikkattu_Rajeevan

I want to share some data between c# and Figma plugin. For example, when c# send name of a node, figmal plugin set that node’s visibility to true and I need to call exportAsync , instead of direct download png, i want to send the bytearray back to c# via some protocol.

How to achieve this?

This topic has been closed for comments

2 replies

Nidhin_Kuttikkattu_Rajeevan
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


Nidhin_Kuttikkattu_Rajeevan

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.


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings