无法加载资源:服务器以状态 404(未找到)响应 socket.io/?EIO=3&transport=polling&t=1503062449710-0



>我终于能够将我的应用程序部署到 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) 

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

服务器端:

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);
});
});

客户端(索引.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( 在应用程序设置中启用了我的 Azure Web 套接字

4( 确保我的连接是正确的站点。

我总是回到同样的错误消息,指出 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完全不同.

我已经研究这个问题已经有一段时间了。

在 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>

有关详细信息,请参阅在 Azure 应用服务中使用 Socket.IO 创建 Node.js 聊天应用程序。

相关内容

最新更新