Uni*_*orn 4 spring-boot angular angular6
我正在实现socketjs。但是我遇到了以下错误。
import * as SockJS from 'sockjs-client';
import * as Stomp from 'stompjs/lib/stomp.js';
Run Code Online (Sandbox Code Playgroud)
提前致谢。
这是我的角度代码-
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();
}
});
Run Code Online (Sandbox Code Playgroud)
});
}
将此添加到您的polyfills.ts中
(window as any).global = window
Run Code Online (Sandbox Code Playgroud)
首先,您需要安装带有类型的套接字客户端
npm install --save @types/socket.io
Run Code Online (Sandbox Code Playgroud)
然后,您可以像这样在您的组件或服务中导入socket.io-client
import * as Socket from 'socket.io-client';
Run Code Online (Sandbox Code Playgroud)
像这样更改代码
let ws = Socket(this.serverUrl);
Run Code Online (Sandbox Code Playgroud)
在index.html的头部添加以下代码段解决了我的问题 -
<script type="application/javascript">
var global = window;
</script>
Run Code Online (Sandbox Code Playgroud)
参考 https://github.com/stomp-js/ng2-stompjs/issues/70
| 归档时间: |
|
| 查看次数: |
2827 次 |
| 最近记录: |