套接字IO网络:: ERR_CONNECTION_REFUSED [英] Socket IO net::ERR_CONNECTION_REFUSED

查看:179
本文介绍了套接字IO网络:: ERR_CONNECTION_REFUSED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的应用程序中实现socket.io,该应用程序托管在Azurewebsites上. webapp

I am trying to implement socket.io into my application which is hosted at Azurewebsites. webapp

这是server.js

Here is the server.js

var app = require('express')();
var server = require('http').createServer(app);

server.listen(process.env.PORT || 3001)

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

var io = require('socket.io')(server);

io.on('connection', function (socket) {
        console.log("Socket connected :"+socket.id);
 socket.emit('news', { hello: 'world' });
});

这是客户端套接字. index.html

And Here is the client side socket. index.html

<script src="socket.io/socket.io.js"></script>
            <script>
        var socket = io('http://localhost:3001');
        console.log("scoekt connect",socket)
      socket.on('connect', function(){ console.log('connected to socket'); });
      socket.on('error', function(e){ console.log('error' + e); });

    socket.on( 'news', function( data ){
    console.log("socket data",data);

    });</script>

我收到以下错误

我不太确定出了什么问题.这是文件系统的结构

I am not really sure whats is going wrong. Here is the structure of file-ing system

ROOT
app/
index.html
server.js
web.config

PS:这是Angular2应用程序

PS: this is an Angular2 application

PS:我已根据此错误检查了所有建议的问题,但没有一个解决了我的问题,因此我正在发布此问题.

PS: I have checked all the suggested question based on this error but none solved my issue, thus i am posting this question.

推荐答案

根据我的经验,Azure Web App不会将loaclhost127.0.0.1绑定到您的网站,只有端口80和443是面向公众的.这会映射到您的应用程序要监听的特定端口,可通过process.env.PORT检索.因此,您需要替换

Per my experience, Azure Web App doesn't bind loaclhost or 127.0.0.1 to your website, and only ports 80 and 443 are public-facing. This maps to a specific port for your app to listen to, retrievable via process.env.PORT. So you'd need to replace

var socket = io('http://localhost:3001');

使用

var socket = io('http://<your app name>.azurewebsites.net');

如果服务器端和客户端在不同的域中,则还需要启用 CORS 在服务器端.在Azure中,我们可以通过Azure门户启用它.

And if your server side and client side in the different domain, you'd also need to enable CORS on the server side. In Azure, we can enable it with the Azure portal.

  • 在浏览器中,转到 Azure门户网站,然后导航至您的App Service.
  • >
  • API 菜单中单击 CORS .
  • 在空白的允许的来源文本框中输入每个网址.创建一个新的文本框.或者,您可以输入星号(*)以指定接受所有原始域.
  • 点击保存.
  • In a browser, go to the Azure portal, and navigate to your App Service.
  • Click CORS in the API menu.
  • Enter each URL in the empty Allowed Origins text box. A new text box is created. As an alternative, you can enter an asterisk (*) to specify that all origin domains are accepted.
  • Click Save.

Socket.IO使用WebSocket,默认情况下未在Azure上启用WebSocket.您还可以使用Azure门户启用WebSocket支持.请参阅以下步骤.

Socket.IO uses WebSockets, which are not enabled by default on Azure. You can also enable WebSocket support using the Azure Portal. Please see the steps below.

  • 在Azure门户中,单击设置菜单中的应用程序设置.
  • Web套接字下,单击打开
  • 点击保存.
  • In the Azure portal, click Application settings in the SETTINGS menu.
  • Under Web Sockets click On
  • Click Save.

有关更多信息,请参考此文档.

For more info, please refer to this documentation.

这篇关于套接字IO网络:: ERR_CONNECTION_REFUSED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆