2016-07-09 27 views
6

Ich benutze angle-cli, um ein neues Projekt zu erstellen. Jetzt möchte ich socketJs zu dem Projekt hinzuzufügen, aber immer Fehler in Browser-Konsole zu halten:Wie man SockJS in Angular 2 Projekt hinzufügen?

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) 

Dies ist meine Schritte und Konfiguration:

typings install sockjs-client --save --ambient 

Winkel-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)' 
    ] 
    }); 
}; 

system 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' 
    } 
}; 

Meine individuelle Serviceklasse:

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'); 
    } 
} 

Bitte lassen Sie mich wissen, wie es zu beheben, vielen Dank!

Ich kann jedoch einige Fehler beseitigen, indem ich die fehlende lib hinzufüge, indem ich sie einzeln in system-config.ts hinzufüge. Aber ich bezweifle, dass dies der richtige Ansatz ist.

+0

es in Ihrem node_module Verzeichnis angezeigt wird? –

+0

Es erscheint. – janetsmith

+1

@anetsmith hast du es herausgefunden? – Birowsky

Antwort

3

Ich habe eine Lösung für dieses Problem gefunden.

Das Problem ist, dass: npm install sockjs-client downloads sockjs-Knoten anstelle von Client. Ich hoffe, sie werden es bald beheben.

In Ihrem Projekt zeigen Sie auf vendor/sockjs-client/lib/entry.js stattdessen sollten Sie diese Datei herunterladen http://cdn.jsdelivr.net/sockjs/1/sockjs.min.js in Ihrem Projekt und zeigen Sie darauf in system-config.ts.

1

In Angular2, der einfachste Weg, sockjs zu importieren verwenden:

import 'your_path/sockjs-1.1.1.js' 

aber bevor Sie zu importieren, bitte Teil des Codes in erster Linie von 'sockjs-1.1.1.js' ändern.

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()} 

ersetzen Sie den Code oben:

window.SockJS = e() 

nach dann SockJS Zugriff von überall sein könnte, weil jetzt eine Eigenschaft Fenster in browser.Also es Sie brauchen Typoskript SockJS zu sagen, ist eine Umgebungsvariable durch Zugabe:

declare let SockJS: any 
2

In einem neuen Version von angular2 CLI https://cli.angular.io/ ist es einfach, Bibliothek hinzuzufügen.

Wenn Sie nur

sockjs_client aufnehmen wollen

1) npm i --save sockjs-client

2) In typings.d.ts diese Erklärung declare module 'sockjs-client';

hinzufügen Aber ich würde empfehlen STOMP-Over-WebSocket-Service für Winkel 2

Installieren Sie dieses npm Pakete

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

In typings.d.ts zu verwenden

Hinzufügen stompjs und sockjs-client Modul Deklaration

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

In app.module.ts

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

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

In 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'); 
    }); 


} 

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

+0

Wie sieht es jetzt aus? – Devsullo

+0

Sieht jetzt besser aus, danke. (Nicht mein Downvote übrigens, mir fehlt Domänenwissen, um darüber abzustimmen.) –