我在使用NativeScript socketio尝试连接到ioBroker上的实例时遇到问题。 没有错误,但也没有建立连接。我尝试通过Web应用程序连接到它,并且它可以正常工作,因此服务器应该没有问题。这是我在NativeScript中所做的:
我建立了此服务:
import { Injectable } from '@angular/core';
import * as SocketIO from "nativescript-socketio";;
import { Observable } from 'rxjs';
import * as Rx from 'rxjs/Rx';
@Injectable({
providedIn: 'root'
})
export class SocketService {
options = {
query: {
token: '',
},
android: {
// http://socketio.github.io/socket.io-client-java/apidocs/io/socket/client/IO.Options.html
},
ios: {
// https://nuclearace.github.io/Socket.IO-Client-Swift/Enums/SocketIOClientOption.html
}
};
private socketio : SocketIO.SocketIO;
connectToSocket(): Rx.Subject<MessageEvent>{
this.socketio = SocketIO.connect('http://192.168.178.50:8085', this.options);
const observable = new Observable(observer => {
this.socketio.on('lampe', data => {
console.log('message from server');
observer.next(data);
});
return () => {
this.socketio.disconnect();
};
});
const observer = {
next: (data:Object) => {
this.socketio.emit('lampe', data);
}
};
return Rx.Subject.create(observer, observable)
}
disconnect(){
this.socketio.disconnect();
}
}
处理程序:
import { Injectable } from '@angular/core';
import { SocketService } from './socket.service';
import { Observable, Subject } from 'rxjs/Rx';
@Injectable({
providedIn: 'root'
})
export class ConnHandlerService {
messages: Subject<any>;
constructor(private socketService: SocketService) {
this.messages = <Subject<any>>this.socketService
.connectToSocket()
.map((response: any): any => {
return response;
});
}
sendmsg(msg){
this.messages.next(msg);
}
}
我的家庭组件中还有一个按钮,它使用this.connHandlerService.sendmsg('val:false');触发sendmsg();