2017-08-18 226 views
0

我終於可以將我的應用程序部署到Azure,但是我總是得到這個與socket.io錯誤無法加載資源:服務器的狀態爲404(未找到)socket.io/?EIO=3&transport=polling&t=1503062449710-0

/socket.io/?EIO=3&transport=polling&t=1503062449710-0 Failed to load resource: the server responded with a status of 404 (Not Found) 

即使它在我的本地主機上工作。

服務器端:

var app = require('express')(); 
var server = require('http').Server(app); 
var io = require('socket.io')(server); 

server.listen(80); 

app.get('/', function (req, res) { 
    res.sendfile(__dirname + '/index.html'); 
}); 

io.on('connection', function (socket) { 
    socket.emit('news', { hello: 'world' }); 
    socket.on('my other event', function (data) { 
    console.log(data); 
    }); 
}); 

客戶端(瀏覽次數index.html):

<html> 

<head> 
    <script src="/socket.io/socket.io.js"></script> 
    <script> 
     var socket = io.connect('http://chattranslatortwo.azurewebsites.net/'); 
     socket.on('news', function(data) { 
      console.log(data); 
      socket.emit('my other event', { 
       my: 'data' 
      }); 
     }); 
    </script> 
</head> 
<body> 
</body> 
</html> 

現在我已經做了很多事情來試圖解決這個問題。

1)由於很多帖子都說要將socket.io更改爲socket.io-client"https://cdn.socket.io/socket.io-1.3.7.js",這對我來說沒有任何改變,結果相同。

2)我試過重新安裝我的node.jssocket.io,但不確定是否改變了任何東西。

3)中啓用確信,我的連接是正確的網站應用程序設置

4)我的Azure的網絡套接字。

我總是回到同樣的錯誤消息唱出socket.io:

/socket.io/?EIO=3&transport=polling&t=1503062449710-0 Failed to load resource: the server responded with a status of 404 (Not Found) 

要麼我完全失去了一些東西,或者它似乎插座上工作WebServices不是從localhost完全不同。

我一直在解決這個問題很長一段時間了。

回答

0

Azure上的應用服務,你需要更改以下行

server.listen(80); 

server.listen(process.env.PORT); 

此外,你應該,如果你的Node.js應用程式的根目錄創建一個web.config不存在。作爲參考,對於使用app.js作爲入口點的應用程序,以下是默認的web.config

<?xml version="1.0" encoding="UTF-8"?> 
<!-- 
     This configuration file is required if iisnode is used to run node processes behind 
     IIS or IIS Express. For more information, visit: 

     https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config 
    --> 
<configuration> 
    <system.webServer> 
    <!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support --> 
    <webSocket enabled="false" /> 
    <handlers> 
     <!-- Indicates that the server.js file is a node.js web app to be handled by the iisnode module --> 
     <add name="iisnode" path="app.js" verb="*" modules="iisnode" /> 
    </handlers> 
    <rewrite> 
     <rules> 
     <!-- Do not interfere with requests for node-inspector debugging --> 
     <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true"> 
      <match url="^app.js\/debug[\/]?" /> 
     </rule> 
     <!-- First we consider whether the incoming URL matches a physical file in the /public folder --> 
     <rule name="StaticContent"> 
      <action type="Rewrite" url="public{REQUEST_URI}" /> 
     </rule> 
     <!-- All other URLs are mapped to the node.js web app entry point --> 
     <rule name="DynamicContent"> 
      <conditions> 
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True" /> 
      </conditions> 
      <action type="Rewrite" url="app.js" /> 
     </rule> 
     </rules> 
    </rewrite> 
    <!-- 
     You can control how Node is hosted within IIS using the following options: 
      * watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server 
      * node_env: will be propagated to node as NODE_ENV environment variable 
      * debuggingEnabled - controls whether the built-in debugger is enabled 

     See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options 
     --> 
    <!--<iisnode watchedFiles="web.config;*.js"/>--> 
    </system.webServer> 
</configuration> 

欲瞭解更多信息,請參閱Create a Node.js chat application with Socket.IO in Azure App Service

相關問題