2017-08-18 214 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

相关问题