I am implementing socketjs. however i have encountered below 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();
}
});
});
}
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
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);
adding following code works for me
<script>
var global = window; // fix global is undefined in socketjs-client
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With