0

When I execute the following code, I get the error: Reference Error: Watershed is not defined. How can I define it? Do I need a module to be installed for it?

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);
});
user3.14
  • 371
  • 2
  • 15

2 Answers2

1

There is also a section of the restify doc that mentioned how to handle the ability to upgrade sockets. I just struggled with this for an emarrassingly long time and thought I'd share the simple solution. In addtion the @Dibu Raj reply, you also need to create your restify server with the handleUpgrades option set to true. Here is a complete example to make restify work with websocket upgrades and watershed:

'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);
});

For an html page to test your new nodejs websocket server: write this html below into a file at ./static/test/index.html - point your browser to http://localhost:8080/test/index.html - open your browser debug console to see the message exchange.

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

Your browser log will look something like this:

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!

And your node log will look like:

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!

Documentation for this found at: http://restify.com/#upgrade-requests

Tim Cederquist
  • 191
  • 3
  • 6
0

You should include the watershed library

var Watershed = require('lib/watershed').Watershed;
Dipu Raj
  • 1,784
  • 4
  • 29
  • 37