2016-11-03 46 views
0

當我執行下面的代碼時,我得到錯誤:參考錯誤:未定義分水嶺。我怎樣才能定義它?我需要爲它安裝一個模塊嗎?如何在Node.js中定義Watershed?

var restify=require('restify'); 
var ws= new Watershed(); 
var server=restify.createServer(); 
server.get('websocket/attach', function upgradeRoute(req, res, next){ 
    if(!res.claimUpgrade){ 
     next(new Error("Connection must be upgraded.")); 
     return; 
    } 
    var upgrade=res.claimUpgrade(); 
    var shed=ws.accept(req, upgrade.socket, upgrade.head); 
    shed.on('text', function (msg){ 
     console.log("The message is: "+msg); 
    }); 
    shed.send("hello there"); 
    next(false); 
}); 
server.listen(8081, function(){ 
    console.log('%s listening at %s', server.name, server.url); 
}); 

回答

0

您應該包括分水嶺庫

var Watershed = require('lib/watershed').Watershed; 
+0

如何你知道嗎?你在哪裏閱讀這些東西? –

0

另外還有一點提到如何處理升級插座的能力的RESTify文檔的一部分。我只是爲了這個漫長的時間而苦苦掙扎,並且認爲我會分享這個簡單的解決方案。另外,@Dibu Raj的回覆,你還需要創建你的restify服務器,並將handleUpgrades選項設置爲true。下面是一個完整的例子,使用的WebSocket升級和流域的RESTify工作:

'use strict'; 
var restify = require('restify'); 
var watershed = require('watershed'); 
var ws = new watershed.Watershed(); 

var server = restify.createServer({ 
    handleUpgrades: true 
}); 

server.get('/websocket/attach', function (req, res, next) { 
    if (!res.claimUpgrade) { 
    next(new Error('Connection Must Upgrade For WebSockets')); 
    return; 
    } 
    console.log("upgrade claimed"); 

    var upgrade = res.claimUpgrade(); 
    var shed = ws.accept(req, upgrade.socket, upgrade.head); 

    shed.on('text', function(msg) { 
    console.log('Received message from websocket client: ' + msg); 
    }); 

    shed.send('hello there!'); 

    next(false); 
}); 

//For a complete sample, here is an ability to serve up a subfolder: 
server.get(/\/test\/?.*/, restify.serveStatic({ 
    directory: './static', 
    default: 'index.html' 
})); 

server.listen(8080, function() { 
    console.log('%s listening at %s', server.name, server.url); 
}); 

一個HTML頁面來測試新的WebSocket的NodeJS服務器:下面寫HTML成./static/test/index文件。 html - 將您的瀏覽器指向http://localhost:8080/test/index.html - 打開瀏覽器調試控制檯查看消息交換。

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title>Web Socket test area</title> 
    <meta name="description" content="Web Socket tester"> 
    <meta name="author" content="Tim"> 
</head> 
<body> 
    Test Text. 
<script> 
    (function() { 
    console.log("Opening connection"); 
    var exampleSocket = new WebSocket("ws:/localhost:8080/websocket/attach"); 
    exampleSocket.onopen = function (event) { 
     console.log("Opened socket!"); 
     exampleSocket.send("Here's some text that the server is urgently awaiting!"); 
    }; 
    exampleSocket.onmessage = function (event) { 
     console.log("return:", event.data); 
     exampleSocket.close(); 
    } 
    })(); 
</script> 
</body> 
</html> 

您的瀏覽器日誌將是這個樣子:

07:05:05.357 index.html:18 Opening connection 
07:05:05.480 index.html:22 Opened socket! 
07:05:05.481 index.html:26 return: hello there! 

而且你的節點日誌將類似於:

restify listening at http://[::]:8080 
client connected! 
Rest service called started 
upgrade claimed 
Received message from websocket client: Here's some text that the server is urgently awaiting! 

文檔本在發現: http://restify.com/#upgrade-requests