2016-07-09 69 views
6

我使用angular-cli來生成一個新項目。現在我想socketJs添加到項目,但不斷收到錯誤在瀏覽器控制檯:如何將SockJS添加到Angular 2項目中?

GET http://localhost:4200/url-parse 404 (Not Found) 
GET http://localhost:4200/inherits 404 (Not Found) 
Unhandled Promise rejection: Error: XHR error (404 Not Found) loading http://localhost:4200/url-parse 
     at XMLHttpRequest.wrapFn [as _onreadystatechange] (http://localhost:4200/vendor/zone.js/dist/zone.js:769:30) 
     at ZoneDelegate.invokeTask (http://localhost:4200/vendor/zone.js/dist/zone.js:356:38) 
     at Zone.runTask (http://localhost:4200/vendor/zone.js/dist/zone.js:256:48) 
     at XMLHttpRequest.ZoneTask.invoke (http://localhost:4200/vendor/zone.js/dist/zone.js:423:34) 
    Error loading http://localhost:4200/url-parse as "url-parse" from http://localhost:4200/vendor/sockjs-client/lib/main.js ; Zone: <root> ; Task: Promise.then ; Value: Error: Error: XHR error (404 Not Found) loading http://localhost:4200/url-parse(…) 
Error: Uncaught (in promise): Error: Error: XHR error (404 Not Found) loading http://localhost:4200/url-parse(…) 
GET http://localhost:4200/json3 404 (Not Found) 
GET http://localhost:4200/debug 404 (Not Found) 
GET http://localhost:4200/events 404 (Not Found) 
GET http://localhost:4200/eventsource 404 (Not Found) 
GET http://localhost:4200/crypto 404 (Not Found) 
GET http://localhost:4200/json3 404 (Not Found) 
GET http://localhost:4200/url-parse 404 (Not Found) 
GET http://localhost:4200/debug 404 (Not Found) 
GET http://localhost:4200/inherits 404 (Not Found) 
Assertion failed: loading or loaded 
GET http://localhost:4200/events 404 (Not Found) 
Assertion failed: loading or loaded 
GET http://localhost:4200/faye-websocket 404 (Not Found) 
Assertion failed: loading or loaded 
GET http://localhost:4200/eventsource 404 (Not Found) 
Assertion failed: loading or loaded 
GET http://localhost:4200/http 404 (Not Found) 
GET http://localhost:4200/https 404 (Not Found) 

這是我的步驟和配置:

typings install sockjs-client --save --ambient 

角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|js.map)', 
     'es6-shim/es6-shim.js', 
     'reflect-metadata/**/*.+(ts|js|js.map)', 
     'rxjs/**/*.+(js|js.map)', 
     '@angular/**/*.+(js|js.map)', 
     'sockjs-client/**/*.+(js)' 
    ] 
    }); 
}; 

系統config.ts

/** Map relative paths to URLs. */ 
const map: any = { 
    'sockjs-client': 'vendor/sockjs-client/lib/entry.js' 
}; 

/** User packages configuration. */ 
const packages: any = { 
    'vendor/sockjs-client/lib': { 
    'format': 'cjs', 
    'defaultExtension': 'js' 
    } 
}; 

我的定製服務類:

import { Injectable } from '@angular/core'; 

import * as SockJS from 'sockjs-client'; 
import BaseEvent = __SockJSClient.BaseEvent; 
import SockJSClass = __SockJSClient.SockJSClass; 

@Injectable() 
export class WebsocketService { 

    private sockJs: SockJSClass; 

    constructor() { 
    console.log('constuctor'); 
    this.sockJs = new SockJS('/hello'); 
    } 
} 

請讓我知道如何解決它,非常感謝!

但是,我可以通過在system-config.ts中逐個添加丟失的lib來消除一些錯誤。但我懷疑這是正確的方法。

const map: any = { 
    'sockjs-client': 'vendor/sockjs-client/lib/entry.js', 
    'json3': 'vendor/sockjs-client/node_modules/json3/lib/json3.js', 
    'url-parse': 'vendor/sockjs-client/node_modules/url-parse/index.js', 
    'inherits': 'vendor/sockjs-client/node_modules/inherits/inherits.js', 
    'debug': 'vendor/sockjs-client/node_modules/debug/debug.js', 
    ... 
}; 
+0

是否會出現在你的node_module目錄? –

+0

它確實出現。 – janetsmith

+1

@janetsmith你弄明白了嗎? – Birowsky

回答

3

我找到了解決這個問題的辦法。

問題是:npm install sockjs-client下載sockjs-node而不是客戶端。我希望他們能儘快解決。

在您的項目中您指向vendor/sockjs-client/lib/entry.js而不是您應該下載此文件http://cdn.jsdelivr.net/sockjs/1/sockjs.min.js把它放在您的項目中,並指向system-config.ts

1

在Angular2,導入sockjs最簡單的方法是使用:

import 'your_path/sockjs-1.1.1.js' 

,但在導入之前,請修改代碼的一部分「sockjs-1.1.1.js」的第一道防線。

if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.SockJS=e()} 

替換上面的代碼:

window.SockJS = e() 

之後再SockJS可以從任何地方訪問,因爲現在是在browser.Also你需要告訴打字稿SockJS窗口的屬性是由環境變量增加:

declare let SockJS: any 
2

在新版本的angular2 CLI https://cli.angular.io/它很容易添加庫。

如果你想添加僅sockjs_client

1)npm i --save sockjs-client

2)在typings.d。TS添加此聲明declare module 'sockjs-client';

但我會建議使用STOMP-過的WebSocket服務的角度2

安裝此NPM包

npm i --save stompjs 
npm i --save sockjs-client 
npm i --save ng2-stomp-service 

typings.d.ts

添加stompjssockjs-client模塊聲明

declare module 'stompjs'; 
declare module 'sockjs-client'; 

app.module.ts

import { StompService } from 'ng2-stomp-service'; 

@NgModule({ 
    ... 
    providers: [StompService] 
}) 

app.components.ts

import { StompService } from 'ng2-stomp-service'; 

private wsConf = { 
    host:'test.com' 
} 

constructor(stomp: StompService) { 

    stomp.configure(this.wsConf); 

    stomp.startConnect().then(() => { 
    console.log('connected'); 
    }); 


} 

https://github.com/devsullo/ng2-STOMP-Over-WebSocket

+0

現在呢? – Devsullo

+0

現在看起來好多了,謝謝。 (不是我的downvote順便說一句,我缺乏領域知識投票在此。) –

相關問題