2017-09-14 85 views
1

我正在與一些Arduino的和運動傳感器的項目工作。 Arduino將他們的數據發送給天藍色的IoThub。現在我有一個工作網頁,收集數據並將其顯示在SVG平面圖中。這個網頁只包含一個html索引和一些JS文件與服務器交談。現在我想在一個ASP.net webapp中實現這個功能來添加更多功能。但現在它來了,我做的和普通網頁完全一樣。同JS-文件等,但現在當我跑我得到一個錯誤的程序:與websockets的錯誤200 javascript

WebSocket connection to 'wss://sensordash-wa.azurewebsites.net/' failed: Error during WebSocket handshake: Unexpected response code: 200

這裏是正常運作的網頁: enter image description here

這裏是不工作asp.net web應用程序與完全相同的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>

+0

是你'Server.js'部署到Azure上的Web應用程序作爲你的WebSocket服務器?我嘗試通過'var socket = new WebSocket(「wss://sensordash-wa.azurewebsites.net」)連接你的端點;'我可以重現你的問題。我試圖建立我的節點。js後端,並部署到天藍色的Web應用程序,然後啓用** Web套接字**在我的Web應用程序的「設置>應用程序設置」下,然後我可以從本地單一的HTML文件接收數據。如果我誤解了你的情況,請糾正我。另外,你可以提供更多細節來縮小這個問題的範圍。 –

+0

是的我的Server.js部署在asp.net應用程序中。我有同樣的問題,我的單個HTML文件工作正常,但是當我在.net應用程序中執行此操作時,出現此問題。 – tijn167

+0

你可以分享你的asp.net應用程序的文件夾結構部署到Azure的Web應用程序? –

回答

1

根據你的描述,我檢查這個問題,並測試了我的蔚藍的web應用程序。根據我的測試,您可以使用url重寫規則來重寫具有由node.js處理的特定路徑的請求。有關更多詳細信息,請參閱以下詳細信息。

的網站內容在我的Azure的Web應用程序的結構:

enter image description here

Web.config文件的URL重寫規則:

enter image description here

MVC視圖頁面:

enter image description here

測試結果:

enter image description here

+0

謝謝先生!奇蹟般有效 – tijn167