2014-10-02 78 views
0

我是node.js和socket.io的新手。node.js在安裝了iisnode的IIS 7.5上發生socket.io錯誤

我在運行IIS 7.5的Window Server 2008 R2上安裝了node.js和iisnode,同時安裝了「tjanczuk」建議的「URL重寫」和「Web Deploy」。

iisnode附帶的例子運行得很好。

但是,我在將socket.io添加到項目時遇到了一些問題。 我通過修改我找到的示例開始。

的URL爲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) 

玩與代碼我能夠通過一個錯誤,但遇到另一個。該代碼波紋管給我對象#Server有沒有方法來配置

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

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

任何指導表示讚賞。

最好的問候。

回答

0

我認爲您未正確加載socket.io。你的代碼在localhost上正常工作,對嗎?我遇到了類似的問題,請看看它:EC2 with socket.io。總之,你需要,他需要連接到到,否則你的代碼

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

提供了socket.io完整的URL會嘗試連接到本地主機。

0

謝謝,

得到它的工作!

以下是所做的更改。

在客戶端:

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

但首先需要運行> 「節點server.js」 如果不是,我得到語法錯誤消息(:-))。