0

I use RequireJS to load some scripts.

Now I would like to create some own methods which also needs the loaded script. I've tried a lot but I always get Uncaught (in promise) ReferenceError: hd is not defined. Both scripts are loaded in my index.html.

Calling the method 'connect' from main.js

hd.connect.call(token);

socket-workers.js

...
require.config({

shim: {
    'socket.io': {
      exports: 'io'
    },
    'socket.io-stream': {
      exports: 'ss'
    }
  },
  paths: {
    'socket.io': WEBSOCKET_PROXY_HOST + '/socket.io/socket.io',
    'socket.io-stream': WEBSOCKET_PROXY_HOST + '/socket.io-stream/socket.io-stream'
  },
  waitSeconds: 1

});

requirejs([
  'socket.io', 'socket.io-stream'
], function(io, ss) {

  // Setup Methods & Events

  const hd = {

    connect : function(token) {
      // Connect to Socket, Proxy Server & authenticate
      socket = io(WEBSOCKET_PROXY_HOST, {
        auth: {
          token: token
        }
      }).on('connect', function(data) {
        socketConnected = true;
        isStreaming = false;
        console.log('connected to socket');
        if (activeRecording) {
          //#startRecording();
        }
      });
...
2
  • 1
    Don't think theres enough info here. Where are you calling hd.connect.call(token)? Commented Dec 11, 2021 at 19:03
  • oh sorry, I have two scripts main.js and socket-workers.js and I want to create a kind of SDK. Commented Dec 11, 2021 at 19:08

2 Answers 2

1

RequireJS can be used this way, but there is a more proper way - define a module and then require it while defining another module:

socket-workers.js:

require.config({

shim: {
    'socket.io': {
      exports: 'io'
    },
    'socket.io-stream': {
      exports: 'ss'
    }
  },
  paths: {
    'socket.io': WEBSOCKET_PROXY_HOST + '/socket.io/socket.io',
    'socket.io-stream': WEBSOCKET_PROXY_HOST + '/socket.io-stream/socket.io-stream'
  },
  waitSeconds: 1

});

define('hd', [
  'socket.io', 'socket.io-stream'
], function(io, ss) {

  // Setup Methods & Events

  const hd = {

    connect : function(token) {
      // Connect to Socket, Proxy Server & authenticate
      socket = io(WEBSOCKET_PROXY_HOST, {
        auth: {
          token: token
        }
      }).on('connect', function(data) {
        socketConnected = true;
        isStreaming = false;
        console.log('connected to socket');
        if (activeRecording) {
          //#startRecording();
        }
      });
    );



    return hd;

main.js:

define(['hd'], function (hd) {
  hd.connect.call(token);
});
Sign up to request clarification or add additional context in comments.

Comments

0

Finally I've found a solution to solve this issue. I've added

window.hd = hd; 

within the function to make the methods available.

Now I can call every method with for e.g. hd.connect(), hd.disconnect()

1 Comment

Without seeing the code in main.js from which the function is called its not clear whether this work around is necessary if the answer itself is kind of meaningless. Intuitively, I would think that hd should be exported from a third module via define as in // hd.js define(['socket.io', 'socket.io-stream'], function (io, ss) { var hd = {...}; return hd; }); and then in main // main.js requirejs(['./hd.js'], function (hd) { hd.connect(token); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.