【问题标题】:connecting to a localhost server in c# app from a firefox extension从 firefox 扩展连接到 c# 应用程序中的本地主机服务器
【发布时间】:2011-04-18 09:41:15
【问题描述】:

我正在使用一个 firefox 扩展程序,它会在每次加载新页面时进行检查,并在加载时向我的 C# 程序中的 Web 服务器发送握手请求。我的问题是服务器永远不会收到请求。有人可以指出我正确的方向,因为我认为我做错了什么。谢谢

function examplePageLoad(event) {

  if (event.originalTarget instanceof HTMLDocument) {

    var win = event.originalTarget.defaultView;

    if (win.frameElement) {


  var socket = new WebSocket('127.0.0.1:13000');
        socket.onopen = function() {
            alert('handshake successfully established. May send data now...');
        };
        socket.onclose = function() {
            alert('connection closed');
        };
    }

  }

}


window.addEventListener("load", function () {

  gBrowser.addEventListener("load", examplePageLoad, true);

}, false);

在 C# 中:

 public void acceptClient()
        {
            TcpListener server = null;
            try
            {
                // Set the TcpListener on port 13000.
                Int32 port = 13000;
                IPAddress localAddr = IPAddress.Parse("127.0.0.1");

                // TcpListener server = new TcpListener(port);
                server = new TcpListener(localAddr, port);

                // Start listening for client requests.
                server.Start();

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


                // Enter the listening loop.
                while (true)
                {
                    Console.Write("Waiting for a connection... ");

                    // Perform a blocking call to accept requests.
                    // You could also user server.AcceptSocket() here.
                    TcpClient client = server.AcceptTcpClient();
                    Console.WriteLine("Event was fired!");


                    UserModel um = new UserModel();

                    um.maintainUserModel(); //method uses to maintain user model

                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            finally
            {
                // Stop listening for new clients.
                server.Stop();
            }

        }

【问题讨论】:

    标签: c# javascript sockets


    【解决方案1】:

    您在浏览器中使用哪个 IP 来访问该页面?环回还是本地IP?必须是同一个 AFAIK。

    在 C# 中使用环回地址时,您不需要查找它。只需使用IPAddress.Loopback

    除此之外,您的服务器没有任何问题。

    附注:

    um.maintainUserModel(); //method uses to maintain user model
    

    不要这样写cmets。这只是实际代码的重复。它没有增加任何价值,它所做的只是把代码文件弄得乱七八糟。

    更新

    此脚本连接良好:

    <script type="text/javascript">
        function examplePageLoad(event) {
            if ("WebSocket" in window) {
                var ws = new WebSocket("ws://localhost:13000/");
                ws.onopen = function() {
                    alert('Sending');
                    ws.send("message to send");
                }
                ws.onmessage = function (evt) { 
                    alert('Received: ' + evt.data);
                };
                ws.onclose = function() { // websocket is closed. 
                };
    
                alert('readystate: ' + ws.readyState + " for " + ws.URL);
            }
            else
                alert('Not supported');
        }
    
        if (window.addEventListener) {
            window.addEventListener("load", examplePageLoad, true);
        }
    </script>
    

    【讨论】:

    • 恐怕还是同样的问题
    • 尝试在浏览器和服务器中使用您的真实 IP。尝试使用 telnet 连接到服务器。
    • 当我在浏览器中使用端口编写环回 ip 时,它可以工作,服务器收到请求。只是当我从脚本中调用它时,它没有。我检查了,事件是有效的,当一个新标签被加载时,事件被启动,但是服务器仍然在等待连接
    • 我不确定环回地址。请使用您的真实IP。
    • 我也试过了 :(。谢谢你的帮助,别担心。我的 javascript 代码一定有问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多