rxjs5 - no WebSocket constructor can be found

穿精又带淫゛_ 提交于 2019-12-12 18:23:24

问题


I'm trying to do something basic, but it's completely eluding me. I'm trying to create an Observable from rxjx/observable/dom/webSocket in RxJS5, but I'm not using typescript, or es6 modules... just plain 'ole good commonJS. I've managed to patch the Observable properly according to the docs, but whenever I try to pass in the string expected for the subject, I get the error:

no WebSocket constructor can be found, [source].

I haven't had time to dig into TypeScript yet, but from what I can tell, I've satisfied the conditions of the constructor, and I've also taken a look at the test spec, and they use the function in the same way Observable.webSocket('ws://host:port'); I'm trying to, but I still get an error.

I've tried:

var Rx = require('rxjs/Rx');
require('rxjs/Rx.dom').webSocketSubject; //also tried just using `.webSocket`

var source = Rx.Observable.webSocket('ws://localhost:53311');

source.subscribe();

I've also tried passing an object to Rx.Observable.webSocket:

var source = Rx.Observable.webSocket({
  url: 'ws://host:port'
});

Can anyone help me figure out how to use the webSocket Observable that's available via rxjs5, when consuming from commonJS? (node v5.11)


回答1:


This isn't about TypeScript, you may need provide constructor to websocket to WebSocketSubject.

In test case, https://github.com/ReactiveX/rxjs/blob/fd0823b99db92d1e214052ad506904b0d744d494/spec/observables/dom/webSocket-spec.ts#L14 specifies it in root to globally override websocket, while it's also possible to provide it as config object like

var ws = require('websocket');

socket = new WebSocketSubject({
  url: 'ws://....',
  WebSocketCtor: ws.w3cwebsocket
});

Even required in TypeScript or ES15 RxJS module as well.



来源:https://stackoverflow.com/questions/36855367/rxjs5-no-websocket-constructor-can-be-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!