在Microsoft Azure上为Socket.io节点应用程序启用Websockets

Whoa my Blood Sugar is a CGM in the Cloud!

NOTE: This is a technical post, I'll blog more about Nightscout later this week. Subscribe and watch for my take, or visit http://www.nightscout.info.

注意:这是一篇技术文章,本周晚些时候我将在博客中详细介绍Nightscout 。 订阅并注意我的拍摄,或访问http://www.nightscout.info

I'm running an application called Nightscout that is a node app with a MongoDB backend that presents a JSON endpoint for a diabetic's blood sugar data. I use my Dexcom G4 CGM (Continuous Glucose Meter) connected with a micro-USB OTG cable to an Android phone. An Android app bridges the device and POSTs up to the website.

我正在运行一个名为Nightscout的应用程序,该应用程序是一个具有MongoDB后端的节点应用程序,该应用程序显示了糖尿病血糖数据的JSON端点。 我使用连接微型USB OTG电缆的Dexcom G4 CGM(连续血糖仪)连接到Android手机。 一个Android应用程序将设备桥接,并将POST发布到网站。

Azure is well suited to run an app like this for a few reasons. Node works great on Azure, MongoLabs is setup in the Azure Store and has a free sandbox, Azure supports WebSockets, and *.azurewebsites.net has a wildcard SSL cert, so I could force SSL.

由于一些原因,Azure非常适合运行这样的应用程序。 Node在Azure上运行良好,MongoLabs在Azure商店中设置,并具有免费的沙箱,Azure支持WebSocket,*。azurewebsites.net具有通配符SSL证书,因此我可以强制使用SSL。

启用Websocket和强制SSL (Enabling Websockets and Forcing SSL)

So my goal here is to do two things, make sure Websockets/socket.io is enabled in my app because it's been using polling, and force my app to use SSL.

因此,我的目标是要做两件事,确保在我的应用程序中启用了Websockets / socket.io,因为它一直在使用轮询,并强制我的应用程序使用SSL。

Setting up a node.js site on Azure is very easy. You can see a 3 minute video on how to do a Git Deploy of a node app here. Azure will see that there's a app.js or server.js and do the right thing.

在Azure上设置一个node.js网站非常容易。 您可以在此处观看有关如何执行节点应用程序的Git Deploy的3分钟视频。 Azure将看到有一个app.js或server.js并做正确的事情。

However, because IIS and node are working together to host the site (IIS hands off to node using a thing called, wait for it, iisnode) you should be aware of the interactions.

但是,由于IIS和节点正在共同协作来托管站点(IIS使用名为iisnode的东西将其移交给节点),因此您应该注意交互。

There's a default web.config that will be created with any node app, but if you want to custom stuff like rewrites, or websockets, you should make a custom web.config. First, you'll need to start from the web.config that Azure creates.

有一个默认的web.config将与任何节点应用程序一起创建,但是如果您要自定义诸如重写或websockets之类的内容,则应制作一个自定义的web.config。 首先,您需要从Azure创建的web.config开始。

Related Link:  Using a custom web.config for Node apps

相关链接:对节点应用程序使用自定义web.config

Let's explore this web.config so we understand what's it's doing so we can enable Websockets in my app. Also, note that even though our project has this web.config in our source repository, the app still works on node locally or hosts like Heroku because it's ignored outside Azure/IIS.

让我们探索这个web.config,以便我们了解它在做什么,因此我们可以在我的应用程序中启用Websockets。 另外,请注意,即使我们的项目在源存储库中包含此web.config,该应用程序仍可在本地节点或Heroku之类的主机上运行,​​因为在Azure / IIS外部会被忽略。

  • Note that we say "webSocket enabled=false" in this web.config. This is confusing, but makes sense when you realize we're saying "disable Websockets in IIS and let node (or whomever) downstream handle it"

    请注意,我们在此web.config中说“ webSocket enabled = false”。 这很令人困惑,但是当您意识到我们说的是“在IIS中禁用Websocket并让下游的节点(或任何人)处理”时,这是有道理的。
  • Note in the iisnode line you'll put path="server.js" or app.js or whatever. Server.js appears again under Dynamic Content to ensure node does the work.

    请注意,在iisnode行中,您将放置path =“ server.js”或app.js或其他内容。 Server.js再次出现在“动态内容”下,以确保节点能够正常工作。
  • I added NodeInspector so I can do live node.js debugging from Chrome to Azure.

    我添加了NodeInspector,以便可以从Chrome到Azure进行实时的node.js调试

  • Optionally (at the bottom) you can tell IIS/Azure to watch *.js files and restart the website if they change.

    (可选)(在底部)您可以告诉IIS / Azure监视* .js文件,并在它们更改时重新启动网站。
  • We also change the special handling of the bin folder. It's not special in the node world as it is in ASP.NET/IIS.

    我们还更改了bin文件夹的特殊处理。 在ASP.NET/IIS中,它在节点世界中并不特殊。
<?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 site to be handled by the iisnode module -->
<add name="iisnode" path="server.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="^server.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 site entry point -->
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
</conditions>
<action type="Rewrite" url="server.js"/>
</rule>
</rules>
</rewrite>

<!-- 'bin' directory has no special meaning in node.js and apps can be placed in it -->
<security>
<requestFiltering>
<hiddenSegments>
<remove segment="bin"/>
</hiddenSegments>
</requestFiltering>
</security>

<!-- Make sure error responses are left untouched -->
<httpErrors existingResponse="PassThrough" />

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

Next, turn on Websockets support for your Azure Website from the configure tab within the Azure Portal:

接下来,从Azure门户中的“配置”选项卡打开对Azure网站的Websockets支持:

Turn on Websockets in the Azure Portal

Now I need to make sure the node app that is using socket.io is actually asking for Websockets. I did this work on my fork of the app.

现在,我需要确保正在使用socket.io的节点应用实际上正在请求Websocket。 我在应用程序的分支上完成了这项工作。

io.configure(function () {
- io.set('transports', ['xhr-polling']);
+ io.set('transports', ['websocket','xhr-polling']);

It turns out the original author only put in one option for socket.io to try. I personally prefer to give it the whole list for maximum compatibility, but in this case, we clearly need Websockets first. When will Websockets fall back if it's unavailable? What Azure website pricing plans support WebSockets?

原来的作者只为socket.io尝试了一个选项。 我个人更喜欢为整个列表提供最大的兼容性,但是在这种情况下,我们显然首先需要Websockets。 如果Websockets不可用,什么时候会回退? 哪些Azure网站定价计划支持WebSockets?

  • Free Azure Websites plans support just 5 concurrent websockets connections. They're free. The 6th connection will get a 503 and subsequent connections will fallback to long polling. If you're doing anything serious, do it in Shared or above, it's not expensive.

    免费的Azure网站计划支持5个并发websocket连接。 他们是免费的。 第6个连接将获得503,随后的连接将回退到长轮询。 如果您正在做任何严重的事情,请使用“共享”或更高版本进行操作,这并不昂贵。

  • Shared Plans support 35 concurrent websockets connections, Basic is 350, and Standard is unlimited.

    共享计划支持35个并发Websocket连接,“基本”为350,“标准”为无限制

You'll usually want to use SSL when using Websockets if you can, especially if you are behind a proxy as some aggressive proxies will strip out headers they don't know, like the Upgrade header as you switch from HTTP to Websockets.

如果可以的话,通常会在使用Websockets时使用SSL,尤其是在代理服务器后面,因为一些攻击性代理会删除不知道的标头,例如从HTTP切换到Websockets时的Upgrade标头。

However, even free Azure websites support SSL under the *.azurewebsites.net domain, so doing development or running a small site like this one gets free SSL.

但是,即使免费的Azure网站在* .azurewebsites.net域下也支持SSL,因此,开发或运行这样的小型网站都可以使用SSL。

I can force it by adding this rule to my web.config, under <system.webServer>/<rewrite>/<rules/>:

我可以通过在<system.webServer> / <rewrite> / <rules />下的web.config中添加此规则来强制执行此操作:

<rule name="Force redirect to https">
<match url="(.*)"/>
<conditions>
<add input="{HTTP_HOST}" pattern=".+\.azurewebsites\.net$" />
<add input="{HTTPS}" pattern="Off"/>
<add input="{REQUEST_METHOD}" pattern="^get$|^head$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/>
</rule>

Note the pattern in this case is specific to azurewebsites.net, and will take any Azure website on the default domain and force SSL. You can change this for your domain if you ike, of course, assuming you have an SSL cert. It's a nice feature though, and a helpful improvement for our diabetes app.

请注意,这种情况下的模式特定于azurewebsites.net,它将采用默认域上的任何Azure网站并强制使用SSL。 当然,您可以根据自己的域名更改此设置,前提是您拥有SSL证书。 不过,这是一个不错的功能,并且对我们的糖尿病应用程序有帮助。

I can confirm using F12 tools that we switched to WebSockets and SSL nicely.

我可以确认使用F12工具可以很好地切换到WebSockets和SSL。

image

The whole operation took about 15 minutes and was a nice compatible change. I hope this helps you out if you're putting node.js apps on Azure like I am!

整个操作耗时约15分钟,是一个很好的兼容更改。 如果您像我一样将node.js应用程序放在Azure上,希望这对您有所帮助!

翻译自: https://www.hanselman.com/blog/enabling-websockets-for-socketio-node-apps-on-microsoft-azure

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值