创建websocket服务和spin服务
This commit is contained in:
@@ -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();
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user