当前位置: 首页>>代码示例>>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;未经允许,请勿转载。