websockets javascript

时间:2017-09-14 13:18:54

标签: asp.net .net azure websocket azure-web-sites

我正在开发一个带有一些Arduino和运动传感器的项目。 arduino将他们的数据发送到天蓝色的IoThub。现在我有一个工作网页收集数据并在SVG平面图中显示。这个网页只包含一个html索引和一些与服务器通信的JS文件。现在我想在ASP.net webapp中实现它,以添加更多功能。但现在它来了,我做的与普通网页完全一样。相同的JS文件等。但是现在当我运行应用程序时出现错误:

  

与'wss://sensordash-wa.azurewebsites.net/'的WebSocket连接失败:WebSocket握手期间出错:意外响应代码:200

以下是工作网页: enter image description here

这是不起作用的asp.net webapp与完全相同的JS文件等: enter image description here

我认为Azure端的一切都运行良好,但我在asp.net应用程序中遗漏了一些东西。 提前谢谢!

Server.js:

const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const moment = require('moment');
const path = require('path');
const iotHubClient = require('Scripts//iot-hub.js');

const app = express();

app.use(express.static(path.join(__dirname, 'public')));
app.use(function (req, res/*, next*/) {
  res.redirect('/');
});

const server = http.createServer(app);
const wss = new WebSocket.Server({ server });

// Broadcast to all.
wss.broadcast = function broadcast(data) {
  wss.clients.forEach(function each(client) {
    if (client.readyState === WebSocket.OPEN) {
      try {
        console.log('sending data ' + data);
        client.send(data);
      } catch (e) {
        console.error(e);
      }
    }
  });
};

var iotHubReader = new iotHubClient(process.env['Azure.IoT.IoTHub.ConnectionString'], process.env['Azure.IoT.IoTHub.ConsumerGroup']);
iotHubReader.startReadMessage(function (obj, date) {
  try {
    console.log(date);
    date = date || Date.now()
    wss.broadcast(JSON.stringify(Object.assign(obj, { time: moment.utc(date).format('YYYY:MM:DD[T]hh:mm:ss') })));
  } catch (err) {
    console.log(obj);
    console.error(err);
  }
});

var port = normalizePort(process.env.PORT || '3000');
server.listen(port, function listening() {
  console.log('Listening on %d', server.address().port);
});

/**
 * Normalize a port into a number, string, or false.
 */

function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
}

文件夹结构: enter image description here

脚本文件夹结构:

enter image description here

web.config:

<?xml version="1.0"?>

<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>


  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="LiveDash" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>

  <system.webServer>
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
      <add name="iisnode" path="server.js" verb="*" modules="iisnode"/>
      <webSocket
     enabled="true"
     receiveBufferLimit="true"
     pingInterval="00:01:00">
      </webSocket>
      
    </handlers>
  </system.webServer>

  <system.web>
    <compilation>
      <assemblies>
        <add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>
  </system.web>
</configuration>

1 个答案:

答案 0 :(得分:1)

根据您的描述,我检查了这个问题,并在我的Azure应用程序上测试了它。根据我的测试,您可以使用url重写规则来重写请求,并使用node.js处理的特定路径。有关详细信息,请参阅以下详细信息。

我的Azure Web App下的网站内容结构:

enter image description here

带有网址重写规则的Web.config:

enter image description here

MVC视图页

enter image description here

测试结果:

enter image description here