本文介绍了如何使用C#应用程序向Firefox扩展发送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经创建了Firefox扩展程序和C#应用程序,并且需要向我的扩展程序发送 String
来执行一些工作
I have created a Firefox extension and a C# application and I need to send a String
to my extension to perform some work
我需要通过HTTP请求(例如套接字)发送它.我已经阅读了有关 nsIServerSocket
的信息,并且无法连接到我的C#应用程序
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(扩展名)代码:
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#(应用程序)代码:
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.
推荐答案
在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");
}
}
在扩展程序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); }
}
这篇关于如何使用C#应用程序向Firefox扩展发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!