Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 - browser-crypto.js:3 Uncaught ReferenceError: global is not defined

I am implementing socketjs. however i have encountered below error.

Angular 6 Socket Error Below are the socket and stomp packages which i am using.

import * as SockJS from 'sockjs-client';
import * as Stomp from 'stompjs/lib/stomp.js';

Thanks in Advance.

Here is my angular code-

import * as Socket from 'socket.io-client';
import * as Stomp from 'stompjs/lib/stomp.js';

initializeWebSocketConnection2(){

let ws = new Socket(this.serverUrl);

this.stompClient = Stomp.over(ws);
let that = this;
this.stompClient.connect({}, function(frame) {


   that.stompClient.subscribe("/test", function(message){ 
        if(message.body) {
          console.log(message.body);
         window.location.reload(); 
        }
 });


   that.stompClient.subscribe("/operation", function(message){ 
        if(message.body) {
          console.log(message.body);
          window.location.reload();

        }
 });

});

}

like image 791
Unicorn Avatar asked Jul 17 '18 11:07

Unicorn


3 Answers

Adding the following snippet in the head of index.html resolved the issue in my case -

<script type="application/javascript">
  var global = window;
</script>

Ref. https://github.com/stomp-js/ng2-stompjs/issues/70

like image 96
V.Vidyasagar Avatar answered Nov 11 '22 17:11

V.Vidyasagar


Add this in your polyfills.ts

(window as any).global = window

First you need to install socket-client with typing

npm install --save @types/socket.io

Then you can import the socket.io-client in your component or service like this

import * as Socket from 'socket.io-client';

Change your code like this

let ws = Socket(this.serverUrl);
like image 39
Chellappan வ Avatar answered Nov 11 '22 16:11

Chellappan வ


adding following code works for me

  <script>
      var global = window; // fix global is undefined in socketjs-client
  </script>
like image 1
Jackie Yu Avatar answered Nov 11 '22 16:11

Jackie Yu