我有一个WebSocket node.js服务器,并成功地从ios,android连接。但是,在angular web应用程序中使用rxjs/WebSocket连接时失败,返回1006。
然后我在本地运行WebSocket服务器,将localhost:4200 angular Web应用程序与服务器连接-> ws://localhost:3000/ws,它工作正常。为什么不能与wss一连接。
伺服器:
const app = express();
const server = createServer(app);
const cors = require('cors');
app.use("/", express.static("public"));
var corsOptions = {
origin: 'http://localhost:4200',
credentials: true
};
app.use(cors(corsOptions));
const wsApp = expressWs(app, server).app;
wsApp.ws("/ws", handleSocketConnection);
const port = process.env.PORT || 3000;
server.listen(port, () => {
console.log(`server started on http://localhost:${port}`);
});
委托方:
public initSocket(clientId): void {
console.log('init socket');
this.socket = webSocket(environment.webSocket);
this.socket.next({ "channel": "login", "name": clientId });
}
public getMessage(): Observable<IWebsocketMessage> {
return new Observable<IWebsocketMessage>(observer => {
this.socket.subscribe((data: IWebsocketMessage) => {
console.log('message received: ' + JSON.stringify(data));
observer.next(data);
}, error => {
console.log(error);
});
});
// return this.socket.asObservable();
}
1条答案
按热度按时间vlf7wbxs1#