當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript Subject.create方法代碼示例

本文整理匯總了TypeScript中rxjs/Rx.Subject.create方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Subject.create方法的具體用法?TypeScript Subject.create怎麽用?TypeScript Subject.create使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在rxjs/Rx.Subject的用法示例。


在下文中一共展示了Subject.create方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: next

const createRxSocket = (connection) => {
  let messages = Rx.Observable.fromEvent(connection, 'message', (message) => JSON.parse(message));
  let messageObserver:any = {
    next(message){
      if(connection.readyState === 1){
        connection.send(JSON.stringify(message));        
      }
     }
  }
  connection.on('close', () => {
    connection.streams && connection.streams.forEach(s => s.unsubscribe());
  })
  return Rx.Subject.create(messages, messageObserver);
}
開發者ID:ranjitjc,項目名稱:ts-express-webapi,代碼行數:14,代碼來源:ws-server.ts

示例2: create

 private create(url): Subject<MessageEvent> {
     let ws = new WebSocket(url);
     let observable = Observable.create(
         (obs: Observer<MessageEvent>) => {
             ws.onmessage = obs.next.bind(obs);
             ws.onerror = obs.error.bind(obs);
             ws.onclose = obs.complete.bind(obs);
             return ws.close.bind(ws);
         }
     );
     let observer = {
         next: (data: Object) => {
             if (ws.readyState === WebSocket.OPEN) {
                 ws.send(JSON.stringify(data));
             }
         },
     };
     return Subject.create(observer, observable);
 }
開發者ID:benf86,項目名稱:samplehangmanfront,代碼行數:19,代碼來源:admin.service.ts

示例3: create

    private create(url): Rx.Subject<MessageEvent> {
        let ws = new WebSocket(url);

        // bind ws events to observable (streams)
        let observable = Rx.Observable.create((obs: Rx.Observer<MessageEvent>) => {
            ws.onmessage = obs.next.bind(obs);
            ws.onerror = obs.error.bind(obs);
            ws.onclose = obs.complete.bind(obs);

            return ws.close.bind(ws);
        }).share();

        // on obs next (send something in the stream) send it using ws.
        let observer = {
            next: (data: Object) => {
                if (ws.readyState === WebSocket.OPEN) {
                    ws.send(JSON.stringify(data));
                }
            },
        };

        return Rx.Subject.create(observer, observable);
    }
開發者ID:bdekk,項目名稱:xentha,代碼行數:23,代碼來源:socket.service.ts

示例4: connect

  public connect(cb: () => void): Subject<Message> {
    console.log('connecting...');
    let self = this;

    let observable = Observable.create((obs: Observer<Message>) => {
      console.log('stomp client...');
      self.client = Stomp.over(new SockJS(`${this.app.contextPath()}/client`));

      // establish a connection to the server and subscribe for game events
      self.client.connect({}, () => {
        console.log('connected!');

        self.client.subscribe(`/user/queue/game`, (msg) => obs.next(msg));

        // notify listeners that we're connected now
        self.stateSubject.next(SocketState.CONNECTED);

        // invoke the connected callback up front
        cb();
      }, (err: any) => {
        console.log(err);

        self.stateSubject.next(SocketState.DISCONNECTED);
      });

      // clean-up by disconnecting if needed
      return () => this.disconnect();
    });

    let observer = {
      next: (message: Message) => {
        self.client.send('/api/game', {}, JSON.stringify(message));
      }
    };

    return Subject.create(observer, observable);
  }
開發者ID:mbpolan,項目名稱:retro-realms,代碼行數:37,代碼來源:socket.service.ts


注:本文中的rxjs/Rx.Subject.create方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。