创建websocket服务和spin服务

This commit is contained in:
zhouyuejun
2018-11-29 10:40:23 +08:00
parent bb2779e6e8
commit 2871120616
12 changed files with 293 additions and 57 deletions
+35
View File
@@ -0,0 +1,35 @@
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
var INSECURE = 'ws://';
var SECURE = 'wss://';
@Injectable()
export class WebsocketService {
public websocket: WebSocket;
constructor() { }
dynamicsMessage() {
let url = ("ws://" + window.location.host + "/v2/broker/?topics=topic2");
if (window.location.protocol === 'https:' && url.indexOf(INSECURE) === 0) {
url = SECURE + url.substr(INSECURE.length);
}
this.websocket = new WebSocket(url);
return new Observable(ob => {
this.websocket.onopen = () => {
console.log('websocket was connected...')
}
this.websocket.onmessage = (event) => {
ob.next(event.data)
}
this.websocket.onerror = () => {
ob.error("连接失败")
}
this.websocket.onclose = () => {
ob.complete();
}
})
}
}