帮酷LOGO
  • 显示原文与译文双语对照的内容
Websocket wrapper for angular2 based on angular-websocket

  • 源代码名称:angular2-websocket
  • 源代码网址:http://www.github.com/afrad/angular2-websocket
  • angular2-websocket源代码文档
  • angular2-websocket源代码下载
  • Git URL:
    git://www.github.com/afrad/angular2-websocket.git
  • Git Clone代码到本地:
    git clone http://www.github.com/afrad/angular2-websocket
  • Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/afrad/angular2-websocket
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
  • angular2websocket

    基于 https://github.com/AngularClass/angular-websocket 并迁移到 Angular2

    安装

    npm install angular2-websocket

    用法:

    import {$WebSocket} from'angular2-websocket/angular2-websocket'var ws =new$WebSocket("url");ws.send(event);

    另外,ws.getDataStream() 返回的主题可以附加观察者( https://github.com/Reactive-Extensions/RxJS )

    编译

    npm run typings
    npm run compile

    二进制类型的默认值为'arraybuffer'。

    示例

    import {$WebSocket, WebSocketSendMode} from'angular2-websocket/angular2-websocket';// connectvar ws =new$WebSocket("ws://127.0.0.1:7000");// you can send immediately after connect, // data will cached until connect open and immediately send or connect fail.// when connect fail, websocket will reconnect or not,// you can set {WebSocketConfig.reconnectIfNotNormalClose = true} to enable auto reconnect// all cached data will lost when connect close if not reconnect// set received message callbackws.onMessage(
     (msg:MessageEvent)=> {
     console.log("onMessage ", msg.data);
     },
     {autoApply: false}
    );// set received message streamws.getDataStream().subscribe(
     (msg)=> {
     console.log("next", msg.data);
     ws.close(false);
     },
     (msg)=> {
     console.log("error", msg);
     },
     ()=> {
     console.log("complete");
     }
    );// send with default send mode (now default send mode is Observer)ws.send("some thing").subscribe(
     (msg)=> {
     console.log("next", msg.data);
     },
     (msg)=> {
     console.log("error", msg);
     },
     ()=> {
     console.log("complete");
     }
     );ws.send("by default, this will never be sent, because Observer is cold.");ws.send("by default, this will be sent, because Observer is hot.").publish().connect();ws.setSendMode(WebSocketSendMode.Direct);ws.send("this will be sent Direct, because send mode is set to Direct.");ws.send("this will be sent and return Promise.", WebSocketSendMode.Promise).then(
     (T) => {
     console.log("is send");
     },
     (T) => {
     console.log("not send");
     }
     );ws.send("this will be sent and return Observer.").subscribe(
     (msg)=> {
     console.log("next", msg.data);
     },
     (msg)=> {
     console.log("error", msg);
     },
     ()=> {
     console.log("complete");
     }
     );ws.close(false); // closews.close(true); // close immediately

    二进制类型

    要设置 web socket的二进制类型,可以在构造函数中将它的作为字符串提供。 允许的类型包括:

    • 'blob ( 默认)
    • 'arraybuffer''
    var ws =new$WebSocket("ws://127.0.0.1:7000", null, null, 'arraybuffer');



    Copyright © 2011 HelpLib All rights reserved.    知识分享协议 京ICP备05059198号-3  |  如果智培  |  酷兔英语