2

I made the signaling server by using webRTC. This is why I made the signaling server(signaling.js).

What should I do to solve this problem?

・signaling.js

"use strict"; 


let WebSocketServer = require('ws').Server;
let port = 3001;
let wsServer = new WebSocketServer({ port: port });
console.log('websocket server start. port=' + port);

wsServer.on('connection', function(ws) {
  console.log('-- websocket connected --');
  ws.on('message', function(message) {
    wsServer.clients.forEach(function each(client) {
      if (isSame(ws, client)) {
        console.log('- skip sender -');
      }
      else {
        client.send(message);
      }
    });
  });
});

function isSame(ws1, ws2) {
  // -- compare object --
  return (ws1 === ws2);
}  let wsUrl = 'ws://localhost:3001/';
  let ws = new WebSocket(wsUrl);
  ws.onopen = function(evt) {
    console.log('ws open()');
  };
  ws.onerror = function(err) {
    console.error('ws onerror() ERR:', err);
  };

  ws.onmessage = function(evt) {
   console.log('ws onmessage() data:', evt.data);
   let message = JSON.parse(evt.data);
   if (message.type === 'offer') {
     // -- got offer ---
     console.log('Received offer ...');
     textToReceiveSdp.value = message.sdp;
     let offer = new RTCSessionDescription(message);
     setOffer(offer);
   }
   else if (message.type === 'answer') {
     // --- got answer ---
     console.log('Received answer ...');
     textToReceiveSdp.value = message.sdp;
     let answer = new RTCSessionDescription(message);
     setAnswer(answer);
   }
 };

 function sendSdp(sessionDescription) {
    console.log('---sending sdp ---');

    textForSendSdp.value = sessionDescription.sdp;
    /*--- テキストエリアをハイライトするのを止める
    textForSendSdp.focus();
    textForSendSdp.select();
    ----*/

    // --- シグナリングサーバーに送る ---
    let message = JSON.stringify(sessionDescription);
    console.log('sending SDP=' + message);
    ws.send(message);
  }

When I load the "signaling.js", I set the node.js. After that, I access signaling.js. However, It listed upgrade required.

I searched how to upgrade node.js.

I tried

Linux/Mac:

The module n makes version-management easy:

sudo npm install n -g
sudo n 0.12.2
For the latest stable version:

sudo n stable
For the latest version:

sudo n latest

after this, I accessed signaling.js

/signaling.js:26
  let ws = new WebSocket(wsUrl);
               ^

ReferenceError: WebSocket is not defined
    at Object.<anonymous> (/Users/Desktop/webRTC/signaling.js:26:16)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3
tk08
  • 167
  • 1
  • 2
  • 10

1 Answers1

1

Looks like you are trying to create a WebSockets client and server in the same script.

/signaling.js:26
  let ws = new WebSocket(wsUrl);
               ^

ReferenceError: WebSocket is not defined

This error occurs because you've forgotten to define the WebSocket variable at the top of your file:

var WebSocket = require('ws');

Since you want to use the same methods as you would inside a web browser, (ws.onopen, ws.onerror, ...), I'd recommend you to use the websocket package instead, with the w3cwebsocket module.

var WebSocket = require('websocket').w3cwebsocket;

After discussing the issue inside the comments, I would like to suggest that you remove completely the 'ws' package and use 'websocket' instead.

var WebSocketServer = require('websocket').server;

Instead of creating a WebSocket server directly, you should create a HTTP server first:

var server = http.createServer(function(request, response) {
    console.log((new Date()) + ' Received request for ' + request.url);
    response.writeHead(404);
    response.end();
});
server.listen(8080, function() {
    console.log((new Date()) + ' Server is listening on port 8080');
});

Then attach the WebSocket server on it:

wsServer = new WebSocketServer({
    httpServer: server,
    autoAcceptConnections: false
});

A new connection will trigger the request event, you'll have to accept the incoming connection:

// wsServer.on('connection', function(ws) {
wsServer.on('request', function(request) {
    var ws = request.accept();

You'll be able to retrieve the clients list with the connections property.

// wsServer.clients.forEach(function each(client) {
wsServer.connections.forEach(function each(client) {
Axel Isouard
  • 1,498
  • 1
  • 24
  • 38
  • thanks, I tried the above. However, there is new error;( node signaling.js module.js:471 throw err; ^ Error: Cannot find module 'websocket' at Function.Module._resolveFilename (module.js:469:15) at Function.Module._load (module.js:417:25) at Module.require (module.js:497:17) at require (internal/module.js:20:19) – tk08 Oct 18 '16 at 08:41
  • I added var WebSocket = require('websocket').w3cwebsocket; the top of file,"singnaling.js". – tk08 Oct 18 '16 at 08:42
  • You need to install the `websocket` package, try to run the following command in a terminal: `npm install --save websocket` – Axel Isouard Oct 18 '16 at 08:45
  • Ok, thanks, I tried npm install --save websocket however , there is error, the same one " upgrade required". so I keep tryng your reference" stackoverflow.com/a/20584128/1025222 " – tk08 Oct 18 '16 at 09:02
  • Try to replace `let WebSocketServer = require('ws').Server;` with `let WebSocketServer = require('websocket').server;`. I've never encountered that error before, it's really weird actually. – Axel Isouard Oct 18 '16 at 09:05
  • I have tried replacing let WebSocketServer = require('ws').Server; with let WebSocketServer = require('websocket').server; this is new error. node signaling.js /Users/sawamuratakahiro/node_modules/websocket/lib/WebSocketServer.js:144 throw new Error('You must specify an httpServer on which to mount the WebSocket server.'); ^ Error: You must specify an httpServer on which to mount the WebSocket server. – tk08 Oct 18 '16 at 09:22
  • For that one, you should try to create an HTTP server and attach the websocket server on it, read more at https://www.npmjs.com/package/websocket#server-example – Axel Isouard Oct 18 '16 at 09:23
  • ummm.... so difficult. I don't know how to change this script about HTTP serer and attach the websocket server on it . – tk08 Oct 18 '16 at 09:54
  • Alright, I'm updating my answer to give a general advice on your code. – Axel Isouard Oct 18 '16 at 09:56
  • 2
    @tk08 Here it is ! – Axel Isouard Oct 18 '16 at 11:32
  • Thanks for your precious time, Axel. I really appreciate you, I retry this program by checking your explanation. you're great Engineer and great man! – tk08 Oct 18 '16 at 11:57