diff --git a/package.json b/package.json index ea4f9ec..46c14fe 100644 --- a/package.json +++ b/package.json @@ -23,23 +23,18 @@ "socket.io-client": "^2.0.2" }, "devDependencies": { - "@angular/common": "^4.1.3", + "@angular/common": "^4.1.3", "@angular/compiler": "^4.1.3", + "@angular/compiler-cli": "^4.1.3", "@angular/core": "^4.1.3", "@angular/platform-browser": "^4.1.3", "@angular/platform-browser-dynamic": "^4.1.3", - "@types/socket.io-client": "^1.4.29", - "core-js": "^2.4.1", - "reflect-metadata": "^0.1.10", - "rxjs": "^5.0.1", - "socket.io": "^1.7.3", - "ts-helpers": "^1.1.1", - "zone.js": "^0.7.2", - "@angular/compiler-cli": "^4.1.3", "@types/jasmine": "2.5.38", "@types/node": "^6.0.63", "@types/socket.io": "^1.4.28", + "@types/socket.io-client": "^1.4.29", "codelyzer": "~2.0.0-beta.1", + "core-js": "^2.4.1", "es6-shim": "^0.35.3", "jasmine": "^2.5.3", "jasmine-core": "2.5.2", @@ -50,10 +45,15 @@ "karma-jasmine": "^1.0.2", "karma-remap-istanbul": "^0.2.1", "protractor": "~4.0.13", + "reflect-metadata": "^0.1.10", + "rxjs": "^5.0.1", "server-destroy": "^1.0.1", + "socket.io": "^1.7.3", "socket.io-client": "^2.0.2", + "ts-helpers": "^1.1.1", "ts-node": "1.2.1", "tslint": "^4.3.0", - "typescript": "^2.3.4" + "typescript": "^2.3.4", + "zone.js": "^0.8.16" } } diff --git a/socket-io.module.ts b/socket-io.module.ts index 288ffe2..ec3715e 100644 --- a/socket-io.module.ts +++ b/socket-io.module.ts @@ -1,4 +1,4 @@ -import { NgModule, ModuleWithProviders } from '@angular/core'; +import { NgModule, ModuleWithProviders, InjectionToken } from '@angular/core'; import { WrappedSocket } from './socket-io.service'; import { SocketIoConfig } from './socketIoConfig'; @@ -9,7 +9,7 @@ export function SocketFactory(config: SocketIoConfig) { return new WrappedSocket(config); } -export const socketConfig: string = "__SOCKET_IO_CONFIG__"; +export const SOCKET_CONFIG_TOKEN = new InjectionToken('__SOCKET_IO_CONFIG__'); @NgModule({}) export class SocketIoModule { @@ -17,11 +17,11 @@ export class SocketIoModule { return { ngModule: SocketIoModule, providers: [ - { provide: socketConfig, useValue: config }, + { provide: SOCKET_CONFIG_TOKEN, useValue: config }, { provide: WrappedSocket, useFactory: SocketFactory, - deps : [socketConfig] + deps : [SOCKET_CONFIG_TOKEN] } ] }; diff --git a/socket-io.service.ts b/socket-io.service.ts index bcfa713..acf4947 100644 --- a/socket-io.service.ts +++ b/socket-io.service.ts @@ -1,17 +1,18 @@ -import { Injectable, EventEmitter } from '@angular/core'; +import { Injectable, EventEmitter, Inject } from '@angular/core'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/share'; import * as io from 'socket.io-client'; import { SocketIoConfig } from './socketIoConfig'; +import { SOCKET_CONFIG_TOKEN } from './socket-io.module'; @Injectable() export class WrappedSocket { - subscribersCounter : number = 0; + subscribersCounter = 0; ioSocket: any; - constructor(config: SocketIoConfig) { + constructor(@Inject(SOCKET_CONFIG_TOKEN) config: SocketIoConfig) { const url: string = config.url || ''; const options: any = config.options || {}; this.ioSocket = io(url, options);