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

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

问题描述

我正在尝试在 Azurewebsites 上托管的应用程序中实现 socket.io.webapp

这里是 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.id);socket.emit('news', { 你好:'world' });});

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

<script src="socket.io/socket.io.js"></script><脚本>var socket = io('http://localhost:3001');console.log("scoekt 连接",socket)socket.on('connect', function(){ console.log('connected to socket'); });socket.on('error', function(e){ console.log('error' + e); });socket.on('新闻',函数(数据){console.log("套接字数据",data);});</脚本>

我收到以下错误

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

ROOT应用程序/索引.html服务器.js网络配置

PS:这是一个 Angular2 应用程序

PS:我已经检查了基于此错误的所有建议问题,但没有解决我的问题,因此我发布了这个问题.

解决方案

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

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

var socket = io('http://<你的应用名称>.azurewebsites.net');

如果您的服务器端和客户端在不同的域中,您还需要启用

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

  • 在 Azure 门户中,单击 设置 菜单中的应用程序设置.
  • Web Sockets下点击开启
  • 点击保存.

更多信息,请参考 本文档.

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

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

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 getting the below error

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: this is an Angular2 application

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

解决方案

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

with

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

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.

  • 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 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.

  • 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天全站免登陆