Socket IO net::ERR_CONNECTION_REFUSED

后端 未结 1 1323
夕颜
夕颜 2021-01-24 07:00

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

Here is the server.js

var app = require(\'ex         


        
相关标签:
1条回答
  • 2021-01-24 07:42

    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.

    0 讨论(0)
提交回复
热议问题