如何在angular 2应用程序中导入socket.io-client?

时间:2016-05-07 14:58:58

标签: javascript angular socket.io

我想在angular 2应用程序中包含sockets.io-client。首先我安装了socket.io-client,安装了打字:

npm install socket.io-client --save
typings install socket.io-client --save --ambient

下一步是将socket.io-client包含到我的index.html中:

 <script src="node_modules/socket.io-client/socket.io.js"></script>

在我的组件中,我正在导入sockets.io:

import * as io from 'socket.io-client'

然后使用它:

var socket = io('http://localhost:3000');
socket.on('event', function(data:any){
   console.log(data);
}.bind(this)); 

这失败了:

zone.js:101 GET http://localhost:3001/socket.io-client 404 (Not Found)
(index):18 Error: Error: XHR error (404 Not Found) loading http://localhost:3001/socket.io-client

有什么想法吗?

4 个答案:

答案 0 :(得分:16)

为了注册模块以便您可以导入它,您需要将它包含在SystemJS配置中。

System.config({
    packages: {
        ...
        "socket.io-client": {"defaultExtension": "js"}
    },
    map: {
        "socket.io-client": "node_modules/socket.io-client/socket.io.js"
    }
});

并将导入更改为:

import io from 'socket.io-client';
    import * as io from "socket.io-client

此外,您不再需要在脚本标记中导入,因此请删除:

<script src="node_modules/socket.io-client/socket.io.js"></script>

最后,如果您想添加这些类型,请添加typings.json

{
  "ambientDependencies": {
    ...
    "socket-io-client":"github:DefinitelyTyped/DefinitelyTyped/socket.io-client/socket.io-client.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd"
  }
}

P.S。在将来,将打字中的哈希值更改为最新的提交哈希值。

答案 1 :(得分:12)

这是一个迟到的答案,因为我遇到了这个问题并安装了正确的类型Converting between half-open and closed ranges为我解决了这个问题:

npm install @types/socket.io-client --save

此包包含socket.io-client的类型定义,因此如果您遇到类型错误,则应该修复它。

答案 2 :(得分:11)

我在尝试将socket.io导入到我的项目时也遇到了问题,这就是我解决它的方法。

我们走了:

1)编辑systemjs.config.js文件:

/**
 * System configuration for Angular 2 samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },
    map: {
      ... here my stuff ...
      "socket.io-client": 'npm:socket.io-client'
    },
    packages: {
      ... here my stuff ...
      "socket.io-client": {
        main: './socket.io.js'
      }
    }
  });
})(this);

2)在shell中打开你的项目然后执行以下行: 通常我们会告诉你:

npm install socket.io-client --save
typings install socket.io-client --save --ambient

但是您可能会收到一条消息,告诉您ambiant标志已弃用,您应该使用global。但是你很快就会发现它也不会起作用。所以我建议你做一些其他事情(好的错误日志会给你提示,但如果你之前没有去过there,你可能会理解它):

typings install dt~socket.io-client --save --global

3)打开需要socket.io的组件,然后在文件顶部添加:

import * as io from "socket.io-client";

然后下去并添加:

export class MessageComponent implements OnInit {
  socket:any = null;

  constructor() {
      this.socket = io('http://localhost:1337');
  }
  ... here my stuff ...
}

其中1337是包含已启动的socket.io的节点服务器的端口。

现在,一切准备就绪,您可以直接提出要求:

this.socket.emit('sendMessage', {content:'it works !'});

希望我帮助:),祝你的项目好运

答案 3 :(得分:1)

angular-cli: 0.0.39 
node: 6.2.2 
os: win32 x64

我尝试将socket.io-client导入到使用angular-cli生成的angular2应用中,但我无法让它工作。

<强> chat.component.ts

import * as io from "socket.io-client";

@Component({
  ...
})
export class ChatAppComponent {
  ...
}

<强>系统config.ts

/** Map relative paths to URLs. */
const map: any = {
    "socket.io-client": "vendor/socket.io-client/socket.io.js"
};

/** User packages configuration. */
const packages: any = {
    "socket.io-client": {"defaultExtension": "js"}
};

<强>角-CLI-build.js

var Angular2App = require('angular-cli/lib/broccoli/angular2-app');

module.exports = function(defaults) {
  return new Angular2App(defaults, {
    vendorNpmFiles: [
      'systemjs/dist/system-polyfills.js',
      'systemjs/dist/system.src.js',
      'zone.js/dist/*.js',
      'es6-shim/es6-shim.js',
      'reflect-metadata/*.js',
      'rxjs/**/*.js',
      '@angular/**/*.js',
      'socket.io-client/socket.io.js'
    ]
  });
};

<强>的package.json

{
      "dependencies": {
        ...
        "socket.io-client": "^1.4.8",
        "systemjs": "0.19.26"
      },
      "devDependencies": {
        ...
        "typescript": "^1.8.10",
        "typings": "
      }
}

<强> typings.json

{
  "ambientDevDependencies": {
    "angular-protractor": "registry:dt/angular-protractor#1.5.0+20160425143459",
    "jasmine": "registry:dt/jasmine#2.2.0+20160412134438",
    "selenium-webdriver": "registry:dt/selenium-webdriver#2.44.0+20160317120654"
  },
  "ambientDependencies": {
    "es6-shim": "registry:dt/es6-shim#0.31.2+20160317120654",
  },
  "globalDependencies": {
    "socket.io-client": "registry:dt/socket.io-client#1.4.4+20160317120654"
  }
}