【问题标题】:node.js with socket.io error on IIS 7.5 with iisnode installed在 IIS 7.5 上安装了 iisnode 的 node.js 与 socket.io 错误
【发布时间】:2014-10-02 21:16:23
【问题描述】:

我是 node.js 和 socket.io 的新手。

我在运行 IIS 7.5 的 Window Server 2008 R2 上安装了 node.js 和 iisnode,还按照“tjanczuk”的建议安装了“URL rewrite”和“Web Deploy”。

iisnode 附带的示例运行良好。

但是我在将 socket.io 添加到项目时遇到了一些问题。 我首先修改了我找到的一个示例。

网址是http://localhost/node/streamData/

这是服务器代码:

var app = require('http').createServer(handler);
var io = require('socket.io').listen(app);

io.configure(function(){
    io.set('resource', 'streamData/server.js'); //Where we'll listen for connections.
});

var fs = require('fs');
var mysql = require('mysql');
var connectionsArray = [];
var connection = mysql.createConnection({
  host     : 'host',
  user     : 'user',
  password : 'password',
  database : 'db' 
    });
var POLLING_INTERVAL = 3000;
var pollingTimer;

// If there is an error connecting to the database
connection.connect(function(err) {
  // connected! (unless `err` is set)
  console.log(err);
});

// creating the server 
app.listen(process.env.PORT||8081);

// on server started we can load our client.html page
function handler(req, res) {
  fs.readFile(__dirname + '/client.html', function(err, data) {
    if (err) {
      console.log(err);
      res.writeHead(500);
      return res.end('Error loading client.html');
    }
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end(data);
  });
}

这里是我的客户:

    <html>
        <head>
            <title>Push notification server streaming on a MySQL db</title>
            <style>
                dd,dt {
                    float:left;
                    margin:0;
                    padding:5px;
                    clear:both;
                    display:block;
                    width:100%;
                }
                dt {
                    background:#ddd;
                }
                time {
                    color:gray;
                }
            </style>
        </head>
        <body>
            <time></time>
            <div id="container">Loading ...</div>
        <!--<script src="node_modules/socket.io/node_modules/socket.io-client/socket.io.js"></script>-->
        <script src="socket.io/socket.io.js"></script>
        <script src="http://code.jquery.com/jquery-latest.min.js"></script>
        <script>

            // create a new websocket
            var socket = io.connect('http://localhost',{recource: 'streamData/server.js'});
            // on message received we print all the data inside the #container div
            socket.on('notification', function (data) {
               // show data...
            });

          });
        </script>
        </body>
    </html>

我收到以下错误:

<p>iisnode encountered an error when processing the request.</p><pre style="background-color: eeeeee">HRESULT: 0x2
    HTTP status: 500
    HTTP subStatus: 1002
    HTTP reason: Internal Server Error</pre><p>You are receiving this HTTP 200 response because <a href=https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config>system.webServer/iisnode/@devErrorsEnabled</a> configuration setting is 'true'.</p><p>In addition to the log of stdout and stderr of the node.exe process, consider using <a href=http://tomasz.janczuk.org/2011/11/debug-nodejs-applications-on-windows.html>debugging</a> and <a href=http://tomasz.janczuk.org/2011/09/using-event-tracing-for-windows-to.html>ETW traces</a> to further diagnose the problem.</p><p>The last 64k of the output generated by the node.exe process to stderr is shown below:</p><pre style="background-color: eeeeee">Application has thrown an uncaught exception and is terminated:
    TypeError: Object #&lt;Server&gt; has no method &apos;configure&apos;
    at Object.&lt;anonymous&gt; (C:\Program Files\iisnode\www\streamData\server.js:5:4)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.&lt;anonymous&gt; (C:\Program Files\iisnode\interceptor.js:210:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)

使用代码我可以传递一个错误,但会遇到另一个错误。下面的代码给我 Object #Server has no method configure

    var io = require('socket.io').listen(app);

    io.configure(function(){
        io.set('resource', 'streamData/server.js'); //Where we'll listen for connections.
    });

感谢任何指导。

最好的问候。

【问题讨论】:

    标签: node.js iis socket.io


    【解决方案1】:

    我认为您没有正确加载socket.io。你的代码在localhost 上运行正常,对吧?我和你有类似的问题,看看它:EC2 with socket.io。简而言之,您需要提供他需要连接到的socket.io 的完整网址,否则使用您的代码

    io.connect('http://localhost',{recource: 'streamData/server.js'})

    将尝试连接到本地主机。

    【讨论】:

      【解决方案2】:

      谢谢,

      让它工作!

      以下是所做的更改。

      在客户端:

          <script src='//localhost:8081/socket.io/socket.io.js'></script>
          var _addr = window.location.protocol + '//' + window.location.host + ':8081';
          var socket = io.connect(_addr);
      

      但首先需要运行 > "node server.js" 如果没有,我会收到语法错误消息(:-))。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-02
        • 1970-01-01
        • 1970-01-01
        • 2013-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多