401使用与node.js的混合中继连接

时间:2017-02-27 14:44:00

标签: javascript node.js azure

我刚刚运行Microsoft的示例代码,使用node.js

测试混合中继连接

运行:

node listener.js

产生以下错误:

errorError: unexpected server response (401)

这是我的代码(node listener.js)...

    const WebSocket = require('hyco-ws');

 const ns = "hcrelay.servicebus.windows.net";
 const path = "hc1";
 const keyrule = "hc1key";
 const key = "Password@1234";

 var wss = WebSocket.createRelayedServer(
     {
         server : WebSocket.createRelayListenUri(ns, path),
         token: WebSocket.createRelayToken('http://' + ns, keyrule,key)
     }, 
     function (ws) {
         console.log('connection accepted');
         ws.onmessage = function (event) {
             console.log(event.data);
         };
         ws.on('close', function () {
             console.log('connection closed');
         });       
 });

 console.log('listening');

 wss.on('error', function(err) {
     console.log('error' + err);
 });

1 个答案:

答案 0 :(得分:1)

我尝试了你的代码并且运行正常。关键是我将keyrule设置为共享访问策略的名称,并将key设置为共享访问策略的主键。

const WebSocket = require('hyco-ws');

const ns = "hcrelay.servicebus.windows.net";  // Relay namespace
const path = "hc1";                           // Hybrid connection name
const keyrule = "RootManageSharedAccessKey";  // Policy name
const key = "sjSqVUo...";                     // Primary key

var wss = WebSocket.createRelayedServer(
 {
     server : WebSocket.createRelayListenUri(ns, path),
     token: WebSocket.createRelayToken('http://' + ns, keyrule,key)
 }, 
 function (ws) {
     console.log('connection accepted');
     ws.onmessage = function (event) {
         console.log(event.data);
     };
     ws.on('close', function () {
         console.log('connection closed');
     });       
});

console.log('listening');

wss.on('error', function(err) {
 console.log('error' + err);
});

enter image description here

enter image description here