如何使用C#应用程序向Firefox扩展发送消息

本文关键字:扩展 消息 Firefox 何使用 应用程序 | 更新日期: 2023-09-27 18:27:40

我已经创建了一个Firefox扩展和一个C#应用程序,我需要向我的扩展发送一个String来执行一些工作

我需要通过像套接字一样的HTTP请求来发送它。我读过关于nsIServerSocket的文章,在连接到我的C#应用程序时遇到了问题

Firefox(扩展)代码:

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#(应用程序)代码:

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!");
    }
}

现在,我有点坚持不下去了。

如何使用C#应用程序向Firefox扩展发送消息

input是一个nsIInputStream实例,您需要读出它:

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);
});

请参阅NetUtil.jsm文档。

不要忘记将onSocketAccepted中的参数名称从socket更改为clientSocket,因为现在您没有定义clientSocket变量。

在C#端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");
                }
            }

在扩展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); }
}