2016-07-09 40 views
6

Uso angular-cli per generare un nuovo progetto. Ora voglio aggiungere socketJs al progetto, ma continuo a ricevere errori nella console del browser:Come aggiungere SockJS al progetto 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) 

Questo è il mio passi e configurazione:

typings install sockjs-client --save --ambient 

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

La mia classe di servizio personalizzato:

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

Per favore fatemi sapere come risolvere il problema, grazie mille!

Tuttavia, è possibile eliminare alcuni errori includendo la libreria mancante aggiungendoli uno alla volta in system-config.ts. Ma dubito che questo sia l'approccio giusto.

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

Non risulta nella directory node_module? –

+0

Appare. – janetsmith

+1

@janetsmith hai capito? – Birowsky

risposta

3

Ho trovato una soluzione a questo problema.

Il problema è quello: npm install sockjs-client download sockjs-node anziché client. Spero che lo risolveranno presto.

Nel tuo progetto stai puntando a vendor/sockjs-client/lib/entry.js invece dovresti scaricare questo file http://cdn.jsdelivr.net/sockjs/1/sockjs.min.js metterlo nel tuo progetto e puntare ad esso in system-config.ts.

1

Nel Angular2, il modo più semplice per importare sockjs è quello di utilizzare:

import 'your_path/sockjs-1.1.1.js' 

ma prima di importare, si prega di modificare parte del codice in prima linea di '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()} 

sostituire il codice qui sopra per:

window.SockJS = e() 

dopo che l'allora SockJS saranno accessibili da qualsiasi luogo, perché ora si tratta di una proprietà di finestra nella browser.Also dovete dire dattiloscritto SockJS è una variabile ambiente da aggiungendo:

declare let SockJS: any 
2

Nella nuova versione del angular2 CLI https://cli.angular.io/ è semplice aggiungere biblioteca.

Se si desidera aggiungere sockjs_client solo

1) npm i --save sockjs-client

2) In typings.d.ts aggiungere questa dichiarazione declare module 'sockjs-client';

ma consiglierei utilizzare STOMP-Over-WebSocket Servizio angolare 2

installare questo pacchetti NPM

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

In typings.d.ts

Aggiungi stompjs e sockjs-client dichiarazione modulo

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


} 

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

+0

Che dire adesso? – Devsullo

+0

Adesso guarda meglio, grazie. (Non è il mio downvote btw, mi manca la conoscenza del dominio per votare su questo.) –