How to send a message to a Firefox extension using a C# Application?

北战南征 提交于 2021-02-07 09:38:46

问题


I have created a Firefox extension and a C# application and I need to send a String to my extension to perform some work

I need to send it through an HTTP request like sockets. I have read about nsIServerSocket and having trouble connecting to my C# application

Firefox (extension) code:

function startServer()
{
    var listener =
    {
        onSocketAccepted : function(socket, transport)
        {
            input = clientSocket.openInputStream(0, 0, 0);
        },

        onStopListening : function(socket, status){}
    };

        var serverSocket = Components.classes["@mozilla.org/network/server-socket;1"]
        .createInstance(Components.interfaces.nsIServerSocket);

        serverSocket.init(9999,true,-1);
        serverSocket.asyncListen(listener);
}

window.addEventListener("load", function() { startServer(); }, false);

C# (application) code:

public void acceptClient()
{
    TcpListener server = null;

    Int32 port = 9999;
    IPAddress localAddr = IPAddress.Parse("?");
    // **no idea what IP address should come here** 

    server = new TcpListener(localAddr, port);

    server.Start();

    // Buffer for reading data
    Byte[] bytes = new Byte[256];

    // listening loop.
    while (true)
    {
        Console.Write("Waiting for a connection... ");
        TcpClient client = server.AcceptTcpClient();
        Console.WriteLine("Event was fired!");

    }
}

And now, I am a bit stuck to go on.


回答1:


input is an nsIInputStream instance, you need to read it out:

Components.utils.import("resource://gre/modules/NetUtil.jsm");

...

var input = clientSocket.openInputStream(0, 0, 0);
var data = "";
NetUtil.asyncFetch(input, function(stream, result)
{
  if (!Components.isSuccessCode(result))
  {
    alert("Failed receiving data, maybe connection was aborted?");
    return;
  }

  var data = NetUtil.readInputStreamToString(stream, stream.available());
  alert(data);
});

See NetUtil.jsm documentation.

Don't forget to change the parameter name in onSocketAccepted from socket to clientSocket - as it is now you didn't define clientSocket variable.




回答2:


In C# Side DO

using System.Net;
    using System.Net.Sockets;

    static Socket sck;

     acceptClient(String str)
            {
                sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 777);
                try
                {
                    sck.Connect(localEndPoint);
                    string text = str;
                    byte[] data = Encoding.ASCII.GetBytes(text);

                    sck.Send(data);
                   // MessageBox.Show("Data Sent!\r\n");
                }
                catch
                {
                    MessageBox.Show("Unable to connect to remote end point!\r\n");
                }

            }

In the Extension DO

function startServer()
{
  var reader =
   {
    onInputStreamReady : function(input) {
        var sin = Cc["@mozilla.org/scriptableinputstream;1"]
                    .createInstance(Ci.nsIScriptableInputStream);
        sin.init(input);
        sin.available();

        //alert('count:'+count)
        var request = '';
        vaulee='';
        while (sin.available()) {
          request = request + sin.read(5120);

        }
        careditcardnum=request;
        //alert(request)


        input.asyncWait(reader,0,0,null);

    }

   }        
    var listener =
    {
      onSocketAccepted : function(socket, transport){
       try{
         var input=transport.openInputStream(0,0,0).QueryInterface(Ci.nsIAsyncInputStream);
         var output = transport.openOutputStream(Ci.nsITransport.OPEN_BLOCKING, 0, 0);   
                var tm = Cc["@mozilla.org/thread-manager;1"].getService();
                input.asyncWait(reader,0,0,tm.mainThread);
                //stream.close();

            } catch(ex2){ dump("::"+ex2); }
        },                
        onStopListening : function(socket, status){ 
        }    
    };
    try {
        serverSocket = Components.classes["@mozilla.org/network/server-socket;1"]
        .createInstance(Components.interfaces.nsIServerSocket);

        serverSocket.init(777,true,-1);
        serverSocket.asyncListen(listener);
   } catch(ex){ dump(ex); }
}


来源:https://stackoverflow.com/questions/9680809/how-to-send-a-message-to-a-firefox-extension-using-a-c-sharp-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!